You can use ComboBoxItem.Content to get the selected text.Here is my demo:
<ComboBox Name="MyCombobox1" Width="120" Height="30" SelectionChanged="MyCombobox1_SelectionChanged">
<ComboBoxItem Content="item1" Tag="tag1"></ComboBoxItem>
<ComboBoxItem Content="item2" Tag="tag2"></ComboBoxItem>
<ComboBoxItem Content="item3" Tag="tag3"></ComboBoxItem>
<ComboBoxItem Content="item4" Tag="tag4"></ComboBoxItem>
</ComboBox>
The event code is:
private void MyCombobox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(MyCombobox1.SelectedItem!=null)
{
//ComboBoxItem cbi1 = (ComboBoxItem)(sender as ComboBox).SelectedItem;
ComboBoxItem cbi = (ComboBoxItem)MyCombobox1.SelectedItem;
string selectedText = cbi.Content.ToString();
}
}
If you use ComboBox with binding data, like below shown:
<ComboBox Name="MyCombobox2" FlowDirection="LeftToRight" DisplayMemberPath="Name" SelectedValuePath="ID" Width="120" Height="30" SelectionChanged="MyCombobox2_SelectionChanged"></ComboBox>
And bind data using code like this:
public class City
{
public int ID { get; set; }
public string Name { get; set; }
}
public MainWindow()
{
InitializeComponent();
List<City> list = new List<City>();
list.Add(new City { ID = 1, Name = "City1" });
list.Add(new City { ID = 2, Name = "City2" });
list.Add(new City { ID = 3, Name = "City3" });
MyCombobox2.ItemsSource = list;
MyCombobox2.SelectedIndex = 0;
}
You can use the below event to get the selected test:
private void MyCombobox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (MyCombobox2.SelectedItem != null)
{
string strID = MyCombobox2.SelectedValue.ToString();
string strName = ((City)MyCombobox2.SelectedItem).Name.ToString();
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.