从C#上的组合框获取内容值

时间:2016-11-24 13:00:53

标签: c# uwp

我们可以在没有数据绑定的情况下从组合框中检索内容吗?这是我的xaml代码

 <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>

我尝试使用choice.SelectedItem.ToString(),但结果将是Windows.UI.XAML.ComboBox,而不是组合框的内容。我想将它传递给列表框,当我使用choice.SelectedValue.ToString()时,它包含与SelectedItem相同的结果。

2 个答案:

答案 0 :(得分:1)

@Olivia Olga Clarissa尝试这个..

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

or

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

答案 1 :(得分:0)

使用choice.SelectedItem.Content,万一你没有看到属性,把它强制转换为((ContentControl)choice.SelectedItem).Content

相关问题