1

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:

Popup Window

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.

Community
  • 1
  • 1
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • "So why can't I somehow cast to / from the right enum based on the selected index value?" I'd guess the answer to that is: _you haven't tried to_ . What prevents you to write three ValueConverters that map an index to an enum value and bind to the ComboBox's `SelectedIndex` instead of `SelectedItem` using these ValueConverters? – Markus Hütter Jun 26 '16 at 13:57
  • @MarkusHütter I was that it was discouraged to handle it via the index. I found another way as per my answer. Thanks. – Andrew Truckle Jun 26 '16 at 14:48

1 Answers1

1

I found an excellent article here that helped me out!

I added the event handlers as described in the above tutorial and was then able to deal with everything!

Sorted.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164