我可以在xaml单独绑定Point的X和Y属性吗?

时间:2016-09-21 15:33:44

标签: c# wpf vb.net xaml

我想分别绑定Point的X和Y属性,可行吗?
如果这一点是对象的属性,可行吗?
创建一个新类并将隐式转换添加到Point,是否可行?
(中文,英文不好,由谷歌翻译)

喜欢这个?@Trifon

<!-- language: lang-c# -->

public class BindingPoint : Animatable
{
    public double X
    {
        get { return (double)GetValue(XProperty); }
        set { SetValue(XProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty XProperty =
        DependencyProperty.Register("MyProperty", typeof(double), typeof(BindingPoint), new PropertyMetadata(0.0));
    public double Y
    {
        get { return (double)GetValue(YProperty); }
        set { SetValue(YProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty YProperty =
        DependencyProperty.Register("MyProperty", typeof(double), typeof(BindingPoint), new PropertyMetadata(0.0));

    public BindingPoint() { }
    public BindingPoint(double x, double y)
    {
        X = x;
        Y = y;
    }

    public static implicit operator Point(BindingPoint bp)
    {
        return new Point(bp.X, bp.Y);
    }
}

它适用于c#代码,例如&#34; Point p = new BindingPoint(1,1); &#34;。
但它在xaml代码中不起作用!

<Path>
    <Path.Data>
        <LineGeometry>
            <LineGeometry.StartPoint>
                <!--Type must be "Point"-->
                <local:BindingPoint X="10" Y="10"/>
            </LineGeometry.StartPoint>
        </LineGeometry>
    </Path.Data>
</Path>

@Clemens我想将Y绑定到每个EndPoint的变化值( value )。
我该怎么办?

<Path StrokeThickness="2" Stroke="Cyan" Canvas.Left="300" xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Path.Resources>
        <sys:Double x:Key="value"/>
    </Path.Resources>
    <Path.Data>
        <GeometryGroup>
            <LineGeometry StartPoint="50,20">
                <LineGeometry.EndPoint>
                    <Point X="30" Y="{StaticResource value}"/>
                </LineGeometry.EndPoint>
            </LineGeometry>
            <LineGeometry StartPoint="50,20">
                <LineGeometry.EndPoint>
                    <Point X="50" Y="{StaticResource value}"/>
                </LineGeometry.EndPoint>
            </LineGeometry>
            <LineGeometry StartPoint="50,20">
                <LineGeometry.EndPoint>
                    <Point X="70" Y="{StaticResource value}"/>
                </LineGeometry.EndPoint>
            </LineGeometry>
        </GeometryGroup>
    </Path.Data>
</Path>

1 个答案:

答案 0 :(得分:0)

创建一个封装Point的新类。声明为新类X和Y的属性。然后,您将能够绑定新类的这些属性。