views:

71

answers:

2

Could somebody please take some time to show me a quick example on how to bind a text box to the property of an object from c# code? (I've tried to do it on my own, but i can't seem to get it right.)

Thank you guys. I just spent an hour before i realized how stupid i am ( i was setting the wrong object as the biding source).

Thank you all for your help.

+1  A: 
Binding b = new Binding();
b.Source = yourObject;
b.Path = new PropertyPath("YourProperty");

yourTextBox.SetBinding( TextBox.TextProperty, b );

There are a lot of other properties on binding you can set. The above does one-way binding, but you can change that by setting the Mode property.

Adam Sills
A: 

Binding the TexeBox Text property with the "Name" property in the ViewModel

Binding binding = new Binding("Name");
binding.Source = ViewModel;
binding.Mode = BindingMode.TwoWay;
SomeTextBox.SetBinding(TextBox.TextProperty, binding);
rantri