Search code examples
c#enumsdrop-down-menu

convert from object back to enum from dropdown (i think)


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>

Solution

  • You could cast to type Input.

    FM.InputPath = (Input)cmbInputPath.SelectedItem;
    

    This will convert from type object to type Input.