我可以分别绑定点xaml的X和Y属性吗?

本文关键字:属性 可以分 绑定 xaml 我可以 | 更新日期: 2023-09-27 18:08:02

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

呢?@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#代码中工作,如"Point p = new BindingPoint(1,1);"。
但它在xaml代码中不起作用!

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

@Clemens我想为每个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>

我可以分别绑定点xaml的X和Y属性吗?

创建一个封装Point的新类。声明为新类X和y的属性,然后就可以绑定这些新类的属性了。