微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在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是一个结构体,结构体不支持继承.从 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();
    }
}

原文地址:https://www.jb51.cc/csharp/100161.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐