[英]Binding a property to a control using Windows Forms Designer
我想使用Windows Forms Designer将属性绑定到控件。
例如,我有此组件:
class MyComponent:Component, INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private string _strProperty;
[Bindable(true)]
public string StrProperty {
get{
return _strProperty;
}
set {
if (_strProperty != value) {
_strProperty = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("StrProperty"));
}
}
}
}
我将这个组件从工具箱中拖放到窗体上。 组件名称为myComponent1。 在同一表格上,我有一个名为textBox1的TextBox控件。
现在,我想将textBox1.Text属性绑定到myComponent1.StrProperty属性。 我知道我可以写代码:
textBox1.DataBindings.Add(new Binding("Text", myComponent1, "StrProperty"));
但我想使用设计器达到相同的结果。 可能吗? 我应该使用BindingSource吗?
由于您的问题已有3年历史了,因此我不确定这在当时是否适用于您的Visual Studio版本,但是对于其他感兴趣的人,这里是您可以使用的方法:
就那么简单。 如果检查生成的设计器代码,您将看到创建的新绑定,就像通过代码完成的那样。
不可以,您只能在WinForm中使用代码隐藏来绑定属性。 但是,您可以在不使用C#代码但使用XAML的WPF中绑定数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.