Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 307
Vitalij Avatar asked Oct 29 '10 11:10

Vitalij


People also ask

What is binding source in WPF?

A binding source is usually a property on an object so you need to provide both the data source object and the data source property in your binding XAML. In the above example the ElementName attribute signifies that you want data from another element on the page and the Path signifies the appropriate property.

How do I bind data in XAML?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.

What is two way binding in WPF?

Two way binding is used when we want to update some controls property when some other related controls property change and when source property change the actual control also updates its property.


1 Answers

Try this

Xaml

<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); 
like image 107
Fredrik Hedblad Avatar answered Sep 18 '22 11:09

Fredrik Hedblad