开发者

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

I have barely any experience with WinForms, but开发者_如何学JAVA 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;
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜