Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将enum转换为bool,以便在Winforms中进行数据绑定?_C#_.net_Winforms_Data Binding - Fatal编程技术网

C# 如何将enum转换为bool,以便在Winforms中进行数据绑定?

C# 如何将enum转换为bool,以便在Winforms中进行数据绑定?,c#,.net,winforms,data-binding,C#,.net,Winforms,Data Binding,有可能这样做吗?我需要使用: this.ControlName.DataBindings.Add (...) 因此,除了将我的enum值绑定到bool之外,我无法定义逻辑 例如: (DataClass) Data.Type (enum) 编辑: 我需要绑定Data.Type,它是复选框的Checked属性的枚举。因此,如果Data.Type是Secure,我希望通过数据绑定来检查SecureCheckbox。如果您绑定到您的类,您可以在其上始终具有如下属性: public bool IsSe

有可能这样做吗?我需要使用:

this.ControlName.DataBindings.Add (...)
因此,除了将我的
enum
值绑定到
bool
之外,我无法定义逻辑

例如:

(DataClass) Data.Type (enum)
编辑:


我需要绑定
Data.Type
,它是复选框的
Checked
属性的枚举。因此,如果
Data.Type
Secure
,我希望通过数据绑定来检查
SecureCheckbox

如果您绑定到您的类,您可以在其上始终具有如下属性:

public bool IsSecured
{
   get 
   {
      if (myEnum == SecEnum.Secured)
         return true;
      else 
         return false;
   }
}

如果需要,只需对setter进行反向操作。

Winforms绑定会生成两个重要且有用的事件:
Format
Parse

format事件在将数据从源拉入控件时激发,Parse事件在将数据从控件拉回数据源时激发

如果处理这些事件,则可以在绑定期间来回更改/重新键入值

例如,以下是这些事件的两个示例处理程序:

 public static void StringValuetoEnum<T>(object sender, ConvertEventArgs cevent)
 {
      T type = default(T);
      if (cevent.DesiredType != type.GetType()) return;
      cevent.Value = Enum.Parse(type.GetType(), cevent.Value.ToString());
 }

 public static void EnumToStringValue<T>(object sender, ConvertEventArgs cevent)
 {
      //if (cevent.DesiredType != typeof(string)) return;
      cevent.Value = ((int)cevent.Value).ToString();
 }

然后在解析过程中将其反转。

也许您可以更具体地说明要绑定到什么?是否将复选框设置为int列?枚举到位列?很抱歉,我现在添加了更多详细信息。return(myEnum==SecEnum.Secured);谢谢,我用INotifyPropertyChanged做了一个类似的,但不起作用。因为我正在更改数据本身,所以没有通知iProperty。我试着自己做,但也没用也适用于泛型类型参数。
 List<NameValuePair> bts = EnumHelper.EnumToNameValuePairList<LegalEntityType>(true, null);
 this.cboIncType.DataSource = bts;                
 this.cboIncType.DisplayMember = "Name";
 this.cboIncType.ValueMember = "Value";

 Binding a = new Binding("SelectedValue", this.ActiveCustomer.Classification.BusinessType, "LegalEntityType");
 a.Format += new ConvertEventHandler(ControlValueFormatter.EnumToStringValue<LegalEntityType>);
 a.Parse += new ConvertEventHandler(ControlValueFormatter.StringValuetoEnum<LegalEntityType>);
 this.cboIncType.DataBindings.Add(a);
 SecEnum se = Enum.Parse(typeof(SecEnum), cevent.Value.ToString());
 cevent.Value = (bool)(se== SecEnum.Secure);