UWP C# ComboBox & the SelectedItem.ToString();

VoyTec 671 Reputation points
2022-11-08T21:53:56.433+00:00

Ok, so... I want to reach the text inside one of the item from the ComboBox, but when I use SelectedItem.ToString() gives me weird Microsoft text, like an error or something.

So, How can I reach content of a selected ComboBox item.

The ComboBox item content of a one of the item won't work for me, since it will give me a tremendously lot of code and work.

Developer technologies | Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Junjie Zhu - MSFT 21,646 Reputation points
    2022-11-09T06:23:39.89+00:00

    Hi @VoyTec ,
    Welcome to Microsoft Q&A!

    You will get a string of Microsoft.xx.xx, indicating that you use some specific types, such as FontFamily in the official document, use SelectedItem.ToString() will get Windows.UI.Xaml.Media.FontFamily.

    Please refer to the following code for the correct and simple way.

    Page.xaml

    <ComboBox  x:Name="TestCombo" Header="Choose" Height="44" Width="296"   
              ItemsSource="{x:Bind datas}"  SelectionChanged="TestCombo_SelectionChanged">           
     </ComboBox>  
    

    Page.xaml.cs

      ObservableCollection<string> datas = new ObservableCollection<string>();  
    
        public MainPage()  
        {  
            this.InitializeComponent();  
    
            datas.Add("Hello");  
            datas.Add("Hello World");  
            datas.Add("Hello New World");  
        }  
    
        private void TestCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)  
        {  
            string strSelected = e.AddedItems[0].ToString();  
            Debug.WriteLine(strSelected);  
        }  
    

    Thank you
    Junjie

    1 person found this answer helpful.

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.