Search code examples
c#xamlwindows-store-apps

How do I get the text, not the name of the comboboxitem, from a combobox with C# in Windows Runtime?


I read many other questions about this and the answers don't work for me. How can I get the text content of a combobox in C#? This is a Windows Store app, not WPF, nor Silverlight.

<ComboBox Name="ColorListSortSelection" Grid.Column="1" Margin="24,5,24,8"
          VerticalAlignment="Top" SelectedIndex="0" HorizontalAlignment="Stretch">
    <ComboBoxItem>Sort By HSB</ComboBoxItem>
    <ComboBoxItem>Sort By SBH</ComboBoxItem>
    <ComboBoxItem>Sort By BHS</ComboBoxItem>
    <ComboBoxItem>Sort By HBS</ComboBoxItem>
    <ComboBoxItem>Sort By SHB</ComboBoxItem>
    <ComboBoxItem>Sort By BSH</ComboBoxItem>
    <ComboBoxItem>Sort By Name</ComboBoxItem>
</ComboBox>

and the C# code to go with it:

object Temp = ColorListSortSelection.SelectedItem;
string Content = Temp.ToString();

The Content string ends up being "Windows.UI.Xaml.Controls.ComboBoxItem", not the string that appears in the box.

The .Content member is private, so I can't access the text that way.

I'm sure that I just did something wrong here, but the solution eludes me.

Dave


Solution

  • I searched for an answer for an hour before asking the question. As usual, the answer presented itself only a moment later:

    <x:String>Sort By HSB</x:String>
    

    The items should not be ComboBox items, they should be strings.

    If anyone wants to comment and let me know more about the "x:" and why that works, the info would be appreciated. is that just a way for the XAML to access OS or language features?

    Thanks.

    Dave