I'm having problems finding a general solution to deal with Enum fields on WPF and WCF, I need a little bit of help. Let's explain with an example:
When creating a person with a Sex enum value [Male, Female] I see three posibilites:
Male is default -> There are two posibilities but one is default. No problems binding the ComboBox.SelectedValue and no problem with WCF serialization, as long as no female feels ofended :)
((Sex)(3)) Undefined value is default -> There are two posibilities but at the begining the user is forced to pick one. ComboBox.SelectedValue correctly sets SelectedIndex = -1, but WCF throws: Enum value '3' is invalid for type 'Sex' and cannot be serialized. Ensure that the necessary enum values are present and are marked with EnumMemberAttribute attribute if the type has DataContractAttribute attribute.
Sex property is nullable -> Its optional to set the Sex and u can even unset it once u pick one.
Unfortunately, case 2 is the most common one but we don't see a simple way to deal with it without making hacks like:
- Define 'Unknown' value for Sex, this value is not valid for the entity, I would consider if it will not be the most common scenario.
- Mark the enum as [Flags] so WCF can codify "Male,Female" as unknown.
- Decorate the sex property with something like [ComboHintUnkwnownOnNew].
- Set SelectedIndex=-1 while keeping the binding some how in WPF.
I know is a thought design decission, not a concrete problem, but I would appreciate some help.