I have seen a lot of resources on this and the results or all varying in approach and complexity. For example:
Binding SelectedValue of ComboBox to enum in WPF
Is it possible to databind to a Enum, and show user-friendly values?
I have 3 sets of enumerations:
public enum StudentGender
{
[XmlEnum(Name ="M")]
Male,
[XmlEnum(Name = "F")]
Female
}
public enum StudentAppointed
{
[XmlEnum(Name = "E")]
Elder,
[XmlEnum(Name = "MS")]
MinisterialServant,
[XmlEnum(Name = "X")]
NotAppointed
}
public enum StudentServing
{
[XmlEnum(Name = "R")]
RegularPioneer,
[XmlEnum(Name = "P")]
Publisher,
[XmlEnum(Name = "U")]
UnbaptisedPublisher,
[XmlEnum(Name = "S")]
Studying
}
I am implementing a popup window which has some combo boxes on it. It is using binding:
Here is an an example of the binding for one of the 3 combo boxes:
<StackPanel Grid.Row="2" Grid.Column="0" Margin="2" Background="WhiteSmoke">
<Label>Serving As:</Label>
<ComboBox x:Name="comboServingAs" SelectedItem="{Binding Serving}" SelectedIndex="2">
<ComboBoxItem>Studying</ComboBoxItem>
<ComboBoxItem>Unbaptized Publisher</ComboBoxItem>
<ComboBoxItem>Publisher</ComboBoxItem>
<ComboBoxItem>Regular Pioneer</ComboBoxItem>
</ComboBox>
</StackPanel>
In the above case, Serving is the following property:
[XmlAttribute(AttributeName ="Publisher")]
public StudentServing Serving { get; set; }
As mentioned, I have seen previous ideas which vary in complexity. But if possible I am trying to avoid have to implement the same thing three times. Can I have one solution that can be applied for all of them?
In each case I need to map the selected combo box item with the associated property enum so that is all works right. I realise it is wrong right now.
It gets more complicated for the Gender combo because I am using Brother/Sister there for reasons too.
I did see about [DescriptionAttribute]
referred to in the above answers too. It just occurs to me that my enums are essentially integers really. So why can't I somehow cast to / from the right enum based on the selected index value?
I hope my question is not too confusing.