0

I've bound an enum to a combobox in WPF using a method a friend showed me instead of everything I've found online. What I need to do is convert the selected item from the dropdown back to an enum because I'm using it to set a property. I'm new to WPF and C# so I'm not even sure I'm properly asking the question, therefor not finding the answer I need.

 private void cmbInputPath_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FM.InputPath = cmbInputPath.SelectedItem;
        }

Here is the property that uses the enum.

public Input InputPath
        {
            set
            {
                switch (value)
                {
                    case Input.DIPLEXER:
                        SetSwitchPosition(SWC3.CurrentPosition, SWC3.P7);
                        break;
                    case Input.AUX1:
                        SetSwitchPosition(SWC3.CurrentPosition, SWC3.P2);
                        break;
                    case Input.AUX2:
                        SetSwitchPosition(SWC3.CurrentPosition, SWC3.P3);
                        break;
                    case Input.AUX3:
                        SetSwitchPosition(SWC3.CurrentPosition, SWC3.P1);
                        break;
                    case Input.AUX4:
                        SetSwitchPosition(SWC3.CurrentPosition, SWC3.P4);
                        break;
                    case Input.AUX5:
                        SetSwitchPosition(SWC3.CurrentPosition, SWC3.P8);
                        break;
                    case Input.AUX6:
                        SetSwitchPosition(SWC3.CurrentPosition, SWC3.P5);
                        break;
                }
            }
        }

Please let me know if I need to show anything else.

Here's the combobox XAML..

<ComboBox x:Name="cmbInputPath" Grid.Row="1" Grid.Column="0" 
                  Text="Select..." IsEditable="True" IsReadOnly="True" 
                  FontWeight="Bold" FontSize="14"
                  VerticalContentAlignment="Center"
                  ItemsSource="{Binding MyInputPath}" SelectionChanged="cmbInputPath_SelectionChanged">
        </ComboBox>
  • Read more about the parsing and other methods you can use with enumerations, here: https://learn.microsoft.com/en-us/dotnet/api/system.enum.getname?view=net-6.0 – Rivo R. Oct 22 '22 at 15:45
  • As an aside, perhaps you may want a `default` exception in the `switch`, see also https://stackoverflow.com/questions/13644737/correct-exception-to-throw-for-an-unhandled-switch-case-for-an-argument – Stefan Wuebbe Oct 22 '22 at 15:46
  • Did you set the properties `DisplayMemberPath` and `SelectedValuePath` of your combobox ? – Rivo R. Oct 22 '22 at 15:48
  • I think what I'm really trying to figure out is how to convert object to enum since it appears the selected item is of type object. – wesley delk Oct 22 '22 at 16:00

2 Answers2

0

You could cast to type Input.

FM.InputPath = (Input)cmbInputPath.SelectedItem;

This will convert from type object to type Input.

  • Wow thank you. That seems way too easy. Question though.... how does it know to convert the type object to the correct Input? – wesley delk Oct 24 '22 at 01:22
  • I am not 100% sure (I am still a student), but I think what is happening is that we are informing the compiler that we know what type the object is, this allows the program to compile without error. If you cast to the incorrect object you will however get a runtime error, which will cause the an exception as soon as it tries to cast the object. – Mervyn Ludick Oct 25 '22 at 06:44
0

More generic solution, I have create one method to convert into every enum. it is easy to manage.

public T ConvertToEnum<T>(object o)
{
   T enumVal= (T)Enum.Parse(typeof(T), o.ToString());
   return enumVal;
}
Pradeep Kumar
  • 1,193
  • 1
  • 9
  • 21