[英]How do I bind a field to a control in WinForms?
假设有一个 class Person {Name},我们想将这个人的名字绑定到 textbox.text 属性。 这是如何实现的?
类似于:
nameTextbox.DataBindings.Add(...)
我已经在 WPF 中使用 DepedencyProperties 完成了此操作,但没有找到类似于 WinForms 的任何内容
这非常简单。 似乎开箱即用。 我一定是瞎了眼,因为没有早点想到这一点
Person person1 = new Person();
person1.Name = "Odys";
textbox.DataBindings.Add("Text", person1, "Name");
工作得很好!
为此,您需要创建一个BindingSource并将 Person 实例用作 DataSource。 然后您必须将 TextBox 文本属性绑定到 BindingSource。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.