In WPF how to get binding of a specific item from the code?

后端 未结 4 1542
春和景丽
春和景丽 2020-12-10 01:50

The example of this would be:

A textBox is bound to some data. There is a second text box which is not bind to anything. So I want to bind text box 2 to the same dat

4条回答
  •  不知归路
    2020-12-10 02:14

    Try this

    Xaml

    
    
    

    Then we can set the binding of the TextProperty for textBox2 to the same as textBox1 with this code behind

    BindingExpression bindingExpression = textBox1.GetBindingExpression(TextBox.TextProperty);
    Binding parentBinding = bindingExpression.ParentBinding;
    textBox2.SetBinding(TextBox.TextProperty, parentBinding);
    

提交回复
热议问题