我们需要在WinForms应用程序中的控件上执行以下操作。
public class BindableDataItem
{
public bool Visible {get; set; }
public bool Enabled {get;set;}
}
现在,我们要将BindableDataItem绑定(bind)到文本框。
这里是绑定(bind)关联。
TextBox.Enabled BindableDataItem.Enabled
TextBox.Visible BindableDataItem.Visible
现在,一个BindableDataItem对象可以与许多具有不同类型的控件关联。
通过调用(BindableDataItem)obj.Enabled = false,应禁用附加到BindableDataItem对象的所有控件。
任何帮助将不胜感激。
最佳答案
这是怎么做的
class MyDataSouce : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private bool enabled=true, visible=true;
public bool Enabled {
get { return enabled; }
set {
enabled= value;
PropertyChanged(this, new PropertyChangedEventArgs("Enabled"));
}
}
public bool Visible {
get { return visible; }
set {
visible = value;
PropertyChanged(this, new PropertyChangedEventArgs("Visible"));
}
}
}
现在,将表单中的控件绑定(bind)到数据源。
MyDataSouce dataSource = new MyDataSouce();
foreach (Control ctl in this.Controls) {
ctl.DataBindings.Add(new Binding("Enabled", dataSource, "Enabled"));
ctl.DataBindings.Add(new Binding("Visible", dataSource, "Visible"));
}
现在您可以启用/禁用控件,例如
dataSource.Enabled = false;