2个依赖属性之间的数据绑定

时间:2010-04-11 20:55:28

标签: wpf data-binding binding

我正在尝试在2个依赖项属性之间进行数据绑定。我想这应该很容易,反正我只是不明白。我已经用谷歌搜索了,但我真的不知道我做错了什么。

我正在尝试将ControlPointProperty绑定到QuadraticBezierSegment.Point1Property,但它不起作用。谢谢你的提示!

 class DataBindingTest : DependencyObject
{
    // Dependency Property
    public static readonly DependencyProperty ControlPointProperty;


    // .NET wrapper
    public Point ControlPoint
    {
        get { return (Point)GetValue(DataBindingTest.ControlPointProperty); }
        set { SetValue(DataBindingTest.ControlPointProperty, value); }
    }


    // Register Dependency Property
    static DataBindingTest()
    {
        DataBindingTest.ControlPointProperty = DependencyProperty.Register("ControlPoint", typeof(Point), typeof(DataBindingTest));
    }


    public DataBindingTest()
    {
        QuadraticBezierSegment bezier = new QuadraticBezierSegment();

        // Binding
        Binding myBinding = new Binding();
        myBinding.Source = ControlPointProperty;
        BindingOperations.SetBinding(bezier, QuadraticBezierSegment.Point1Property, myBinding);

        // Test Binding: Change the binding source
        ControlPoint = new Point(1, 1);


        MessageBox.Show(bezier.Point1.ToString()); // gives (0,0), should be (1,1)
    }
}

1 个答案:

答案 0 :(得分:1)

Source不是要绑定的属性,而是源对象。 这有效:

Binding myBinding = new Binding("ControlPoint");
myBinding.Source = this;