Get selected value from combo box in C# WPF
My XAML is as below:
<ComboBox Grid.Row="2" Grid.Column="1" Height="25" Width="200" SelectedIndex="0" Name="cmbDeviceDefinitionId"> <ComboBoxItem Content="United States" Name="US"></ComboBoxItem> <ComboBoxItem Content="European Union" Name="EU"></ComboBoxItem> <ComboBoxItem Content="Asia Pacific" Name="AP"></ComboBoxItem></ComboBox>
The content is showing as text and the name of the WPF combobox. To get the name of the selected item, I have follow this line of code:
ComboBoxItem ComboItem = (ComboBoxItem)cmbDeviceDefinitionId.SelectedItem;string name = ComboItem.Name;
To get the selected text of a WPF combobox:
string name = cmbDeviceDefinitionId.SelectionBoxItem.ToString();