I have below WPF combobox:
<ComboBox x:Name="MyComboBox"
Grid.Column="1"
SelectionChanged="MyComboBox_SelectionChanged">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBoxItem Name="cbiEmployeeManagerType">
<StackPanel Orientation="Horizontal">
<Image Source="/Resources/Manager.png" />
<TextBlock Foreground="AliceBlue"
VerticalAlignment="Center">Manager</TextBlock>
</StackPanel>
</ComboBoxItem>
<ComboBoxItem Name="cbiEmployeeEngineerType">
<StackPanel Orientation="Horizontal">
<Image Source="/Resources/Engineer.png" />
<TextBlock Foreground="AliceBlue"
VerticalAlignment="Center">Engineer</TextBlock>
</StackPanel>
</ComboBoxItem>
</ComboBox>
First problem:
Within MyComboBox_SelectionChanged
I know how to detect which item is currently selected in the combobox through MyComboBox.SelectedIndex
but I do not know how to get the text that appears displayed and currently selected in the combobox. For example, If I select the second item in my combobox, I want to obtain "Engineer". How can I do it?
Second problem:
Also I would like to do the same as in combobox winforms in which you can display a member (DisplayMember
property of combobox in winforms) and internally to associate it a member value (ValueMember
property of combobox in winforms) which you can read when you selected the item within the combobox. For example suppose the following combox items with their associated values.
- "Manager" : 1000A
- "Engineer" : 1000B
So "Manager" and "Engineer" would be displayed in the combox and when I would select "Manager" I would obtain its associated value, that is, 1000A, the same for Engineer. Is it possible in WPF? If so how? I have read that it is possible using DisplayMemberPath
and SelectedValuePath
combobox properties but I do not know how to do it. Do I need to create a class and populate the combo from there and then using binding? Any piece of code will be highly appreciated.
UPDATE: For second problem finally I have done similar to what is explained here and here.