c# - How to convert enum to a bool for DataBinding in Winforms? -
is possible this? need use:
this.controlname.databindings.add (...)
so can't define logic other bind enum
value bool
.
for example:
(dataclass) data.type (enum)
edit:
i need bind data.type
enum checkbox's checked
property. if data.type
secure
, want securecheckbox
checked, through data binding.
winforms binding generates 2 important , useful events: format
, parse
.
the format event fires when pulling data source control , parse event fires when pulling data control data source.
if handle these events can alter/retype values going , forth during binding.
for example here couple of example handlers these events:
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(); }
and here code attaching these event handlers:
list<namevaluepair> bts = enumhelper.enumtonamevaluepairlist<legalentitytype>(true, null); this.cboinctype.datasource = bts; this.cboinctype.displaymember = "name"; this.cboinctype.valuemember = "value"; binding = 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);
so in case can create secenum bool handler format event , within like:
secenum se = enum.parse(typeof(secenum), cevent.value.tostring()); cevent.value = (bool)(se== secenum.secure);
and reverse during parse.
Comments
Post a Comment