views:

68

answers:

2

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 object 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.

+1  A: 

in order for binding to work, this BindableDataItem must implement INotifyPropertyChange Interface. have you done this?

Benny
I have the INotifyPropertyChange implemented. Thanks. I think, I have to use the DataBindings.Add(...)
Gopalakrishnan Subramani
+1  A: 

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;
affan