Data Binding and controls
We have the following operation to be performed on the control in our WinForms application.
public class BindableDataItem
{
public bool Visible {get; set; }
public bool Enabled {get;set;}
}
Now we want to bind the BindableDataItemto a TextBox.
Here are binding association.
TextBox.Enabled <==> BindableDataItem.Enabled
TextBox.Visible <==> BindableDataItem.Visible
Now one BindableDataItem objec开发者_运维问答t may associated with many controls with different type.
By calling (BindableDataItem) obj.Enabled = false should disable all the controls attached to the BindableDataItem object.
Any help shall be appreciated.
This is how it is done
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"));
}
}
}
Now bind the controls in your form to the your datasource.
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"));
}
Now you can enable/disable controls e.g
dataSource.Enabled = false;
in order for binding to work, this BindableDataItem must implement INotifyPropertyChange Interface. have you done this?
精彩评论