简体   繁体   中英

Databinding with Control Properties in Winforms: Bind Enabled to state of Combobox?

I have barely any experience with WinForms, but I'm fairly sure that this is a simple task. I just need to enable and disable the Enabled property of a textbox based on the SelectedIndex of a ComboBox.

Can this be done in the designer using DataBindings, or am I required to write a handler of some kind?

You can bind it, but you have to write a Value -> Boolean converter to do the logic. I would suggest since winforms doesn't support the ViewModel paradigm you just go with an event handler as you'd likely have to define your databind in code anyway.

public void MyComboBox_SelectedIndexChanged(object sender, EventArgs args)
{
   ComboBox box = sender as ComboBox;
   if (box != null) return;

   switch(box.Text)
   {
      case "Value1":
      case "Value2":
      case "Value3":
         myTextBox.Enabled = false;
         break;
      default:
         myTextBox.Enabled = true;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM