加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:58:31 所属栏目:百科 来源:网络整理
导读:我知道多个继承已经出来了,但有没有办法为System. Windows.Point创建一个包装器,它可以继承它但仍然实现可绑定的依赖属性? 我正在尝试编码,以便对于我的XAML,我可以创建如下的staments而不会出现问题: custom:Point X =“{Binding Width,ElementName = Pa
我知道多个继承已经出来了,但有没有办法为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是一个结构体,结构体不支持继承.从 MSDN起

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();
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读