Olivia Olga Clarissa Olivia Olga Clarissa - 11 days ago 5
C# Question

Getting content value from combo box on C#

can we retrieve the content from combobox without data binding? This is my xaml code

<ComboBox x:Name="Choice" Header="Choice your eating time" PlaceholderText="Pilih" Width="200" SelectionChanged="Choice_SelectionChanged">
<ComboBoxItem Content="Breakfast" />
<ComboBoxItem Content="Lunch" />
<ComboBoxItem Content="Dinner"/>
</ComboBox>


I tried with
choice.SelectedItem.ToString()
but the result will be Windows.UI.XAML.ComboBox, not the content of combobox. I want to pass it into listbox and when I used
choice.SelectedValue.ToString()
it contain same result with SelectedItem.

Answer

@Olivia Olga Clarissa try this ..

Text = ((ComboBoxItem)Choice.SelectedItem).Content.ToString();

or

var comboBoxItem = Choice.Items[Choice.SelectedIndex] as ComboBoxItem;
if (comboBoxItem != null)
{
    string selectedcmb = comboBoxItem.Content.ToString();
}
Comments