开发者

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 开发者_StackOverflowobject 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜