2017-09-17 32 views

回答

0

您需要通過實現INotifyCollectionChanged的SortedDictionary創建包裝類。

簡單的例子(當然你需要實現字典的所有方法):

public class SyncSortedDictionary<T1,T2> : INotifyCollectionChanged, IDictionary<T1,T2> 
{ 
    #region Fields 

    private readonly SortedDictionary<T1,T2> _items; 

    #endregion 

    #region Events 

    public event NotifyCollectionChangedEventHandler CollectionChanged; 

    #endregion 

    #region Notify 

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

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

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

    private void OnCollectionChanged(NotifyCollectionChangedEventArgs e) 
    { 
     var collectionChanged = CollectionChanged; 

     if (collectionChanged == null) 
      return; 

     collectionChanged(this, e); 
    } 

    #endregion 

    #region Public Methods 

    public void Add(KeyValuePair<T1, T2> item) 
    { 
     int index = _items.Count; 

     _items.Add(item.Key, item.Value); 

     OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index); 
    } 

    #endregion 
} 
+0

謝謝,但如何在主窗口的ListBox控件收聽或綁定到這個?對不起,我是新來的wpf。 – quacksquared

相關問題