2017-09-17 3 views
0

MainWindowとMainWindowのコードビハインドに埋め込みサブウィンドウがあります。埋め込まれたサブウィンドウには、独自のコードビハインドファイルもあります。メインウィンドウには、サブウィンドウで見つかった文字列のリストをユーザーがダブルクリックするたびに反映させるリストボックスがあります。リストボックスをWPFのSortedDictionaryから追加または削除するたびにリストボックスを自動的に反映させるには

これを行うにはどうすればよいですか?私はINotifyCollectionChangedを見てきましたが、msdnのドキュメントは非常にまばらです。

助けてください。

答えて

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

おかげで、どのようにメインウィンドウ上のリストボックスコントロールがに耳を傾けるか、これに結合していますか?すみません、私はwpfを初めて使っています。 – quacksquared

関連する問題