【发布时间】:2017-02-01 07:34:15
【问题描述】:
我有一个组合框 cmbOptions 和一个按钮 btnShowItem
这是代码:
private void btnShowItem_click(object sender, RoutedEventArgs e)
{
string item = ((ComboBoxItem)cmbOptions.SelectedItem).Content.ToString(); //Exception is here
}
以下是例外:
System.InvalidCastException:“无法将 'System.String' 类型的对象转换为 'System.Windows.Controls.ComboBoxItem' 类型。”
我已经浏览过很多这样的链接:
Cannot get ComboBox selected item value
ComboBox- SelectionChanged event has old value, not new value
Get selected value from combo box in c# wpf
等等等等。
但没有得到解决方案。
请注意,我需要在 buttonclick 上获取 comboboxItem 的值,而不是在 cmbSelectionChange 事件上
【问题讨论】:
-
您是在使用绑定还是手动添加了几个
ComboBoxItems? -
@ManfredRadlwimmer 手动