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

aza*_*zad 1 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; } 在此先感谢您的帮助!

Hai*_*OUI 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
            }
        }
Run Code Online (Sandbox Code Playgroud)