开发者

Databinding enums to checkboxes C#

开发者 https://www.devze.com 2023-01-25 02:26 出处:网络
Does anyone know how one can data bind enum field which is a combination of ORed base enum? Say I have

Does anyone know how one can data bind enum field which is a combination of ORed base enum? Say I have

MyEnumType
{ 
    e1=0x1, 
    e2=0x2, 
    e3=0x4
}

and then I do

MyEnumType myEnum = MyEnumType.e1 | MyEnumType.e3

Then I would like to display a dialog box which can be used to set value of myEnum. Right now I have a bunch of checkboxes each of which corresponds to a different value in enum type. Depending on what is checked I cy开发者_JAVA百科cle through them and so on. No data binding done, all is manual.


Google for FlaggedEnumEditor. It will be a good enough example to understand the concept.


I would create a helper class (view model) that:

  • Had properties called E1, E2, etc
  • The class will implement IPropertyChanged
  • The set method of each property will update the underlying value, the get method will read from the underlying value.
  • Whenever the underlying value is changed, you will need to work out which properties have changed and call IPropertyChanged correctly.

Then just databind the check boxes to the properties.

However if I had more than one such enum, I would start looking for solution that did not need me to write so much code.

0

精彩评论

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