在C#WPF中创建可绑定的Point

Ros*_*ber 7 c# wpf inheritance dependency-properties

我知道多个继承已经出来了,但有没有办法为System.Windows.Point创建一个包装器,它可以继承它但仍然实现可绑定的依赖属性?

我正在尝试编码,以便我的XAML可以创建如下的staments而不会出现问题:

<custom:Point X="{Binding Width, ElementName=ParentControlName}" Y="{Binding Height, ElementName=ParentControlName}" />

它可以使像Polygons,Paths,LineSegments和其他控件这样的编码变得更加容易.

以下代码是作为一厢情愿的想法提供的,我理解它不会起作用,但这是我希望能够做到的事情:

public class BindablePoint: DependencyObject, Point
{
    public static readonly DependencyProperty XProperty =
    DependencyProperty.Register("X", typeof(double), typeof(BindablePoint),
    new FrameworkPropertyMetadata(default(double), (sender, e) =>
    {
        BindablePoint point = sender as BindablePoint;
        point.X = (double) e.NewValue;
    }));

    public static readonly DependencyProperty YProperty =
    DependencyProperty.Register("Y", typeof(double), typeof(BindablePoint),
    new FrameworkPropertyMetadata(default(double), (sender, e) =>
    {
        BindablePoint point = sender as BindablePoint;
        point.Y = (double)e.NewValue;
    }));

    public new double X
    {
        get { return (double)GetValue(XProperty); }
        set 
        { 
            SetValue(XProperty, value);
            base.X = value;
        }
    }

    public new double Y
    {
        get { return (double)GetValue(YProperty); }
        set
        {
            SetValue(YProperty, value);
            base.Y = value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Fre*_*lad 5

不幸的是,Point是一个结构体,并且结构体不支持继承。来自MSDN

注意 结构不支持继承,但它们可以实现接口。有关更多信息,请参阅 接口(C# 编程指南)

也许这不能直接回答您的问题,但您可以Point在转换器的帮助下轻松绑定 a 。在你的情况下,它会像

<SomeControl.SomePointProperty>
    <MultiBinding Converter="{StaticResource PointConverter}">
        <Binding ElementName="ParentControlName"
                 Path="Width"/>
        <Binding ElementName="ParentControlName"
                 Path="Height"/>
    </MultiBinding>                                        
</SomeControl.SomePointProperty>
Run Code Online (Sandbox Code Playgroud)

点转换器

public class PointConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double xValue = (double)values[0];
        double yValue = (double)values[1];
        return new Point(xValue, yValue);
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您只想绑定 X 值并具有静态 Y 值,您可以这样做

<SomeControl SomePointProperty="{Binding Path=Width,
                                         ElementName=ParentControlName,
                                         Converter={StaticResource PointXConverter},
                                         ConverterParameter=20}"/>
Run Code Online (Sandbox Code Playgroud)

PointX转换器

public class PointXConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double progressBarValue = (double)value;
        double yValue = System.Convert.ToDouble(parameter);
        return new Point(progressBarValue, yValue);
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
} 
Run Code Online (Sandbox Code Playgroud)

  • @mzabsky 我不是在争论结构的有用性或其相关性。我只是想知道为什么在这个特定的上下文中使用它。例如,`RotateTransform` 具有属性 `CenterX` 和 `CenterY` 而不是 Center `Point`。如果我说要为旋转设置动画或将其字段链接到其他类和控件,则可以更轻松地绑定这些值。他们在那里选择退出“Point”,我敢打赌,因为如果他们不这样做,管理绑定会困难得多。 (2认同)