[英]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.