在我的.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; }
在此先感谢您的帮助!
你可以像下面这样
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
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句