How to get selected text in combobox.

zleug 51 Reputation points
2020-09-14T20:12:31.407+00:00

WPF form has ComboBox where null value associated text "-- Please select --".
I tryed ComboBox1.Text. It gives wrong value.

How to extract selected text from selected combobox item?

Thanks.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
{count} votes

Accepted answer
  1. DaisyTian-1203 11,626 Reputation points
    2020-09-15T02:14:41.53+00:00

    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.


1 additional answer

Sort by: Most helpful
  1. zleug 51 Reputation points
    2020-09-15T14:15:01.617+00:00

    Hi DaisyTian-MSFT. Thanks for try to help.
    That is XAML of ComboBox

        <ComboBox x:Name="EmployeeComboBox" Margin="235 0 0 0" FontSize="14" Width="265" Height="25" ToolTip="Employee" SelectionChanged="EmployeeComboBox_SelectionChanged" 
            IsSynchronizedWithCurrentItem="True" IsEditable="True" IsReadOnly="True" Style="{StaticResource ST_FlatComboBox}" TabIndex="0"
            DisplayMemberPath="Name" SelectedValuePath="Id">
            <ComboBox.SelectedValue>
                <Binding Path="Id" NotifyOnValidationError="True" Mode="TwoWay">
                    <Binding.ValidationRules>
                        <local:ValidateComboBox ObjectType="String" />
                    </Binding.ValidationRules>
                </Binding>
            </ComboBox.SelectedValue>
        </ComboBox>
    </StackPanel>
    

    Thanks.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.