2010-09-29 76 views
7

我已經使用WPF的ObservableCollection進行綁定,並且工作得很好。我現在真正想要的是像一個字典,它有一個我可以使用的關鍵字,像「ObservableCollection」一樣有效。WPF - 我如何使用一個鍵(如字典)實現ObservableCollection <K,T>?

你能提出可以用來提供這樣一個ObservableCollection的代碼嗎?我們的目標是讓我可以從WPF綁定一個類似於結構的詞典。

感謝

回答

1

創建一個類實現IDictionary,INotifyCollectionChanged & INotifyPropertyChanged的接口。該類將有一個Dictionary實例用於實現IDictionary(其中一個Add方法編碼如下)。無論INotifyCollectionChanged和INotifyProperyChanged需要事件的情況下,這些事件應該在適當的點在包裝功能解僱(再次,諮詢爲例添加下面的方法)

class ObservableDictionary<TKey, TValue> : IDictionary, INotifyCollectionChanged, INotifyPropertyChanged 
{ 
    private Dictionary<TKey, TValue> mDictionary; 
    // Methods & Properties for IDictionary implementation would defer to mDictionary: 
    public void Add(TKey key, TValue value){ 
     mDictionary.Add(key, value); 
     OnCollectionChanged(NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, value) 
     return; 
    } 
    // Implementation of INotifyCollectionChanged: 
    public event NotifyCollectionChangedEventHandler CollectionChanged; 
    protected void OnCollectionChanged(NotifyCollectionChangedEventArgs args){ 
     // event fire implementation 
    } 
    // Implementation of INotifyProperyChanged: 
    public event ProperyChangedEventHandler ProperyChanged; 
    protected void OnPropertyChanged(PropertyChangedEventArgs args){ 
     // event fire implementation 
    } 
} 

編輯:

注意的實現IDictionary接口的直接或間接地將需要三個額外的接口來實現:

ICollection<KeyValuePair<TKey,TValue>> 
IEnumerable<KeyValuePair<TKey,TValue>> 
IEnumerable. 

根據您的需求,你可能沒有實現整個的IDictionary INTE如果你只打算調用幾個方法,那麼只需實現這些方法,IDictionary接口就變成了一種奢侈。您必須實現INotifyCollectionChanged和INotifyPropertyChanged接口才能進行綁定.Blockquote

+0

把它放到VS我注意到有更多的接口方法需要實現 - 這是正確的嗎? – Greg 2010-09-29 02:58:17

+0

是的,如果您的要求要求您實施整個IDictionary界面,請參閱我的編輯 – 2010-09-29 03:28:45

0

什麼是這樣的:

ObservableCollection<Tuple<string, object>>() 

其中字符串對象和樣品類型當然

+1

根據使用情況,這可能有效。但是,考慮如何在使用這樣的元組時通過鍵'找到'元素。字典可以使用散列查找按鍵的值,並且可以比搜索匹配鍵的列表快得多。 – MPavlak 2016-08-10 13:54:37

+0

您提到了一點。然而,使用可觀察的集合來處理巨大的數據集可能不是一個好主意。 – eka808 2016-08-16 00:17:23

0

公共類ObservableDictonary:字典,INotifyCollectionChanged,INotifyPropertyChanged的 { 公共事件NotifyCollectionChangedEventHandler CollectionChanged;

public event PropertyChangedEventHandler PropertyChanged; 

    public new void Add(TKey key, TValue value) 
    { 
     base.Add(key, value); 
     if (!TryGetValue(key, out _)) return; 
     var index = Keys.Count; 
     OnPropertyChanged(nameof(Count)); 
     OnPropertyChanged(nameof(Values)); 
     OnCollectionChanged(NotifyCollectionChangedAction.Add, value, index); 
    } 

    public new void Remove(TKey key) 
    { 
     if (!TryGetValue(key, out var value)) return; 
     var index = IndexOf(Keys, key); 
     OnPropertyChanged(nameof(Count)); 
     OnPropertyChanged(nameof(Values)); 
     OnCollectionChanged(NotifyCollectionChangedAction.Remove, value, index); 
     base.Remove(key); 
    } 

    public new void Clear() 
    { 

    } 

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) 
    { 
     PropertyChanged?.Invoke(this, e); 
    } 

    protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e) 
    { 
     CollectionChanged?.Invoke(this, e); 
    } 

    private void OnPropertyChanged(string propertyName) 
    { 
     OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); 
    } 

    private void OnCollectionChanged(NotifyCollectionChangedAction action, object item) 
    { 
     OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item)); 
    } 

    private void OnCollectionChanged(NotifyCollectionChangedAction action, object item, int index) 
    { 
     OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item, index)); 
    } 

    private int IndexOf(KeyCollection keys, TKey key) 
    { 
     var index = 0; 
     foreach (var k in keys) 
     { 
      if (Equals(k, key)) 
       return index; 
      index++; 
     } 
     return -1; 
    } 
} 

我ovveride添加,刪除和清除。您必須瞭解,如果您使用擴展方法或採用Dictonary參數的簡單方法,則不會看到更改,因爲在此情況下,添加或刪除方法將使用Dictonary(而不是ObservableDictonary)。所以你必須直接使用方法(添加或刪除)ObservableDictonary

相關問題