2011-10-12 76 views
2

相同的項目,如果I M使用以下類型的代碼,然後WPF組合框的SelectionChanged事件在不觸發WPF的SelectionChanged事件組合框不點火,如果它有它

cmbFunctionsList.Items.Add("sameItem"); 
cmbFunctionsList.Items.Add("sameItem"); 
cmbFunctionsList.Items.Add("sameItem"); 
cmbFunctionsList.Items.Add("sameItem"); 
cmbFunctionsList.Items.Add("sameItem"); 

有任何工作圍繞這一點。

+0

我有這樣的使用身邊的一些其他工作obj所到cmbFunctionsList的ItemSource分配數據表,並添加新的GUID的每一行也做了string.Equals方法返回true數據表:(但我覺得卡爾的方法好得多。 – Abhi

回答

2

嘗試這樣做:從here

2

這當然是一個奇怪的問題。我能想到的唯一解決方法是存儲組合框的索引,並且每次發生任何事情時(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壞;)

+0

謝謝克里斯蒂安 – Abhi

5

WPF組合框採取

ComboBoxItem newItem = new ComboBoxItem(); 
newItem.Content = "same item"; 
cmbFunctionsList.Items.Add(newItem); 

理念不會改變所選項目如果當前選定的項目和新選擇的項目被認爲相等,則新調用的對象(即newlyslected.Equals(currentlySelected))上調用的方法爲object.Equals()

在這種情況下,由於字符串的值相等

相關問題