简体   繁体   中英

What is the correct way to bind a textbox to an object

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.

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM