[英]ComboBox SelectionChanged event firing when we changing value of combobox WPF
[英]SelectionChanged Event of WPF comboBox is not firing if it has same items in it
如果我使用以下类型的代码,则WPF comboBox的SelectionChanged事件不会触发
cmbFunctionsList.Items.Add("sameItem");
cmbFunctionsList.Items.Add("sameItem");
cmbFunctionsList.Items.Add("sameItem");
cmbFunctionsList.Items.Add("sameItem");
cmbFunctionsList.Items.Add("sameItem");
这有什么工作吗?
如果通过在新选择的对象上调用的newlyslected.Equals(currentlySelected)
object.Equals()
方法(即, newlyslected.Equals(currentlySelected)
)调用当前选定的项目和所选的新项目,则WPF组合框不会更改所选项目。
在这种情况下, string.Equals
方法返回true,因为字符串的值相等
这当然是一个奇怪的问题。 我能想到的唯一解决方法是存储组合框的索引,每次发生任何事情时(KeyDown,LeftMouseButtonDown等)都会根据新索引检查存储的索引。 就像是:
public MainWindow()
{
InitializeComponent();
//populate combo box
lastKnownIndex = comboBox1.SelectedIndex;
}
int lastKnownIndex;
private void comboBox1_KeyDown(object sender, KeyEventArgs e) // and all other possible input events
{
if (comboBox1.SelectedIndex != lastKnownIndex)
{
//do stuff
lastKnownIndex = comboBox1.SelectedIndex;
}
}
可能有一个更优雅的解决方案,但这应该工作。
编辑:也应该让MSFT知道WPF坏了;)
试着这样做:
ComboBoxItem newItem = new ComboBoxItem();
newItem.Content = "same item";
cmbFunctionsList.Items.Add(newItem);
从这里采取的想法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.