user1205746
user1205746

Reputation: 3364

Bind "Enabled" properties of controls to a variable

I am running into an issue that I have found on some similar post, however, they are not quite the same and I am not quite sure how to apply it to my scenario. They may or may not be the same as my case. So, I am posting my own question here hopefully, I will get an answer to my specific scenario.

Basically, I have a window form with a bunch of controls. I would like to have the ability to bind their Enabled property to a Boolean variable that I set so that they can be enable or disable to my discretion.

public partial class MyUI : Form
{
    private int _myID;
    public  int  myID 
    {
        get
        {
            return _myID;;
        }
        set
        {
            if (value!=null)
            {
                _bEnable = true;
            }
        }
    }
    private bool _bEnable = false;
    public bool isEnabled
    {
        get { return _bEnable; }
        set { _bEnable = value; }
    }

    public myUI()
    {
        InitializeComponent();
    }

public void EnableControls()
{
   if (_bEnable)
   {
    ctl1.Enabled = true;
    ctl2.Enabled = true;
            ......
    ctl5.Enabled = true;
   }
       else
   {
    ctl1.Enabled = false;
    ctl2.Enabled = false;
            ......
    ctl5.Enabled = false;
       }
}
}

}

The method EnableControls above would do what I need but it may not be the best approach. I prefer to have ctrl1..5 be bound to my variable _bEnable. The variable will change depending on one field users enter, if the value in the field exists in the database, then other controls will be enabled for user to update otherwise they will be disabled.

I have found a very similar question here but the data is bound to the text field. How do I get rid of the EnableControls method and bind the value of _bEnabled to the "Enabled" property in each control?

Upvotes: 4

Views: 11192

Answers (2)

Selman Genç
Selman Genç

Reputation: 101701

Maybe you can try this approach: in your isEnabled property's setter method, add an if statement:

if(_bEnable) EnableControls();
else DisableControls();

And if your control names are ctl1,ctl2... etc. you can try this:

EnableControls()
{
   for(int i=1; i<6;i++)
   {
      string controlName = "ctl" + i;
      this.Controls[controlName].Enabled = true;
   }
}

And apply the same logic in DisableControls

If you have more controls in future this could be more elegant.

Upvotes: 0

Todd Bowles
Todd Bowles

Reputation: 1574

Go look into the MVVM (Model - View - ViewModel) pattern, specifically its implementation within Windows Forms. Its much easier to apply it to a WPF/Silverlight application, but you can still use it with Windows Forms without too much trouble.

To solve your problem directly, you will need to do 2 things:

  1. Create some class that will hold your internal state (i.e. whether or not the buttons are enabled). This class must implement INotifyPropertyChanged. This will be your View Model in the MVVM pattern.
  2. Bind an instance of the class from 1.) above to your Form. Your form is the View in the MVVM pattern.

After you have done 1 and 2 above, you can then change the state of your class (i.e. change a property representing whether a button is enabled from true to false) and the Form will be updated automatically to show this change.

The code below should be enough to get the concept working. You will need to extend it obviously, but it should be enough to get you started.

View Model

public class ViewModel : INotifyPropertyChanged
{
    private bool _isDoStuffButtonEnabled;
    public bool IsDoStuffButtonEnabled
    {
        get
        {
            return _isDoStuffButtonEnabled;
        }
        set
        {
            if (_isDoStuffButtonEnabled == value) return;
            _isDoStuffButtonEnabled = value;
            RaisePropertyChanged("IsDoStuffButtonEnabled");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

View

public class View : Form
{
    public Button DoStuffButton { get; set; }

    public void Bind(ViewModel vm)
    {
        DoStuffButton.DataBindings.Add("Enabled", vm, "IsDoStuffButtonEnabled");
    }
}

Usage

public class Startup
{
    public ViewModel ViewModel { get; set; }
    public View View { get; set; }

    public void Startup()
    {
        ViewModel = new ViewModel();
        View = new View();

        View.Bind(ViewModel);

        View.Show();

        ViewModel.IsDoStuffButtonEnabled = true;

        // Button becomes enabled on form.

        // ... other stuff here.
    }
}

Upvotes: 11

Related Questions