Vitalij Vitalij - 1 year ago 93
C# Question

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

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 data 1st textBox is bound.

In other words I wan't to know if the DependencyObject stores some reference to it's data-bindings? If not, what is the way to find out all data-bindings of a specific object?

Answer Source

Try this


<TextBox Name="textBox1" Text="{Binding Text1}"/>
<TextBox Name="textBox2" Text="No Binding"/>

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download