简体   繁体   English

在Winforms中具有控件属性的数据绑定:是否已绑定到Combobox的状态?

[英]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. 我几乎没有WinForms的经验,但是我很确定这是一个简单的任务。 I just need to enable and disable the Enabled property of a textbox based on the SelectedIndex of a ComboBox. 我只需要基于ComboBox的SelectedIndex启用和禁用文本框的Enabled属性。

Can this be done in the designer using DataBindings, or am I required to write a handler of some kind? 可以在设计器中使用DataBindings完成此操作,还是需要编写某种处理程序?

You can bind it, but you have to write a Value -> Boolean converter to do the logic. 您可以绑定它,但是必须编写一个Value-> Boolean转换器来执行逻辑。 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. 我建议您这样做,因为Winforms不支持ViewModel范例,您只需要使用事件处理程序,因为无论如何您可能都必须在代码中定义数据绑定。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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