开发者

How do I bind a field to a control in WinForms?

开发者 https://www.devze.com 2023-03-28 14:23 出处:网络
Assume there is a class Person {Name} and we want to bind the name of the person to a textbox.text property.

Assume there is a class Person {Name} and we want to bind the name of the person to a textbox.text property. How is this accomplished?

something similar to:

nameTextbox.DataBindings.Add(...)

I have done this in WPF using DepedencyProperties but haven't find any开发者_JAVA技巧thing similar to WinForms


This is amazingly simple. Seems to be working out of the box. I must bee blind for not figuring this earlier

Person person1 = new Person();
person1.Name = "Odys";
textbox.DataBindings.Add("Text", person1, "Name");

Works just fine!


You need to create a BindingSource and use the Person instance as DataSource for this. Then you have to bind the TextBox Text Property to the BindingSource.

0

精彩评论

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