理由はありませんObservableKeyedCollection<TKey, TValue=""> できます。純させていただきます。

StackOverflow https://stackoverflow.com/questions/2369767

質問

きます。NETフレームワークを含む以降のバージョン3.0の ObservableCollection<T>, ではなぜジョンがObservableKeyedCollection<TKey, TValue="">.

でも実施す自分のコレクションから派生する KeyedCollection<TKey,TValue> 及び実施に INotifyCollectionChanged インタフェースがwhouldntできます。NET Framework.

役に立ちましたか?

解決

理由はありませんObservableKeyedCollection(その他のタイプするだけの組み合わせその他の汎用型) ObservableCollection であるが、その実施"ObservableKeyedCollection"とこと:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

public class DictionaryWatcher : ObservableCollection<KeyValuePair<string, object>>, IDisposable
{
    private NotifyCollectionChangedEventHandler watcher;
    private bool watching = false;

    public DictionaryWatcher()
    {
        watcher = new NotifyCollectionChangedEventHandler( ReportChange );
        CollectionChanged += watcher;
        Watched = true;
    }

    public bool Watched
    {
        get
        {
            return watching;
        }

        set
        {
            if (watching)
            {
                lock (this)
                {
                    CollectionChanged -= watcher;
                    watching = false;
                }
            }
        }
    }

public void Dispose()
{
    Dispose( true );
    GC.SuppressFinalize( this );
}

    public void Initialize()
    {
        this.Add( new KeyValuePair<string, object>( "First", 1 ) );
        this.Add( new KeyValuePair<string, object>( "Second", 2 ) );
        this.Add( new KeyValuePair<string, object>( "Turd", 3 ) );
        KeyValuePair<string, object> badValue = this[2];
        this.Remove( badValue );
    }

protected virtual void Dispose( bool disposing )
{
    if (disposing && Watched)
    {
        Watched = false;
    }
}

    private void ReportChange( object sender, NotifyCollectionChangedEventArgs e )
    {
        Console.WriteLine( "Change made: {0}", e.Action );
    }
}

がありませんライナープログラムでは定型.何よりもされているとは思いませんが、再実装を行うの ObservableCollection してきたこと;なので存分に活用します。

の理由で"whouldnないもの.NETフレームワーク"ができあうものを別について教えてくださいはん。の少ない方があり得た、ある特定のタスクの少ない方がいが低調に推移しました。8)

のツールを提供しているすべての方法を使用します。

こ!

他のヒント

ご覧ください、 ObservableKeyedCollectionクラスの実装.いです。

私はあなたが C5 のを見てお勧めします。それはAddedInsertedRemovedRemovedAtCleared、およびChanged含めたそのコレクションのすべてのための標準として、申し出に観察コレクションという素晴らしいジェネリックコレクションライブラリです。また、C5のコレクションは、すべての理想を「インターフェイスへのプログラミング」を支持します。インターフェースの全ては、基礎となる実装-System.Collections.Generic名前空間に欠けているの完全な機能を公開提供します。また、徹底したのドキュメントでは、。私は非常にそれをチェックアウトすることをお勧めします。

scroll top