Get selected value from combo box in C# WPF Get selected value from combo box in C# WPF wpf wpf

Get selected value from combo box in C# WPF


I have figured it out a bit of a strange way of doing it compared to the old WF forms:

ComboBoxItem typeItem = (ComboBoxItem)cboType.SelectedItem;string value = typeItem.Content.ToString();


Well.. I found a simpler solution.

String s = comboBox1.Text;

This way I get the selected value as string.


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();