我有一个 ComboBox,它的 ComboBoxItem 都是在运行时(以编程方式)生成的。每当出现 ComboBox.SelectionChange 时,程序将显示一个 MessageBox,显示 ComboBox
private void cb2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show(cb2.SelectedItem.ToString());
}
但是,它告诉我:
System.Windows.Controls.ComboBoxItem: Hello World
我只想显示“Hello World”而不是“System...”。我尝试了 SelectedValue,它也显示了同样的东西。
请您参考如下方法:
您需要将所选项目转换为 ComboBoxItem 并仅获取其内容。
MessageBox.Show((cb2.SelectedItem as ComboBoxItem).Content.ToString());
