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

前端之家收集整理的这篇文章主要介绍了在C#WPF中创建可绑定的Point前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道多个继承已经出来了,但有没有办法为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",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;
        }
    }
}

解决方法

不幸的是,Point是一个结构体,结构体不支持继承.从 @L_403_1@起

Note Structs do not support
inheritance,but they can implement
interfaces. For more information,see
07001.

也许这不会直接回答你的问题,但你可以在转换器的帮助下轻松绑定一个Point.在你的情况下,它会是这样的

<SomeControl.SomePointProperty>
    <MultiBinding Converter="{StaticResource PointConverter}">
        <Binding ElementName="ParentControlName"
                 Path="Width"/>
        <Binding ElementName="ParentControlName"
                 Path="Height"/>
    </MultiBinding>                                        
</SomeControl.SomePointProperty>

PointConverter

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,System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

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

<SomeControl SomePointProperty="{Binding Path=Width,ElementName=ParentControlName,Converter={StaticResource PointXConverter},ConverterParameter=20}"/>

PointXConverter

public class PointXConverter : IValueConverter
{
    public object Convert(object value,System.Globalization.CultureInfo culture)
    {
        double progressBarValue = (double)value;
        double yValue = System.Convert.ToDouble(parameter);
        return new Point(progressBarValue,yValue);
    }
    public object ConvertBack(object value,System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
原文链接:https://www.f2er.com/csharp/100161.html

猜你在找的C#相关文章