C#UWP如何获取更改后的ComboBoxItem的值

时间:2016-04-14 12:52:59

标签: c# combobox

在我的.xaml文件中,我的组合框如下所示:

<ComboBox Name="CLengthCombo" SelectionChanged="ComboBox_SelectionChanged"> <ComboBoxItem Content="24"/> <ComboBoxItem Content="25"/> <ComboBoxItem Content="26" IsSelected="True"/> <ComboBoxItem Content="27"/> </ComboBox> 如何实现我的ComboBox_SelectionChanged事件,以便我可以获取在应用程序运行时由用户更改的comboBoxItem的内容? SelectionChanged事件是否正确甚至在这种情况下使用?以下不起作用:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { string chosenItem = CLengthCombo.PlaceholderText; } 在此先感谢您的帮助!

3 个答案:

答案 0 :(得分:5)

你可以像下面这样做

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var comboBoxItem = e.AddedItems[0] as ComboBoxItem;
            if (comboBoxItem == null) return;
            var content = comboBoxItem.Content as string;
            if (content != null && content.Equals("some text"))
            {
                //do what ever you want
            }
        }

答案 1 :(得分:0)

您可以使用组合框

的SelectedItem属性
(CLengthCombo.SelectedItem as ComboBoxItem).Content

https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.combobox.aspx#properties

答案 2 :(得分:0)

让你的组合框工作,c。(...)有SelectedItem,SelectedText ......:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var c = sender as ComboBox;

    var item = c.(...);
}