0

いくつかのオブジェクトのリストがあります。以下のように:

public ObservableCollection<Property > Items { get; private set; }

クライアントがアイテム i を要求したときに、i の代わりにアイテム i+1 を送信する必要があります。リストがいっぱいになったときに配置を変更することはできません。また、ユーザーが i の代わりに Items を呼び出すときに、i+1 を呼び出すことはできません。

したがって、最善の方法はget、このプロパティのメソッドをオーバーライドして、 i のインデックスにあるオブジェクトの代わりに i+1 のインデックスにあるオブジェクトを返すことです。出来ますか?

4

1 に答える 1

1

提案されているように、に基づいて独自のコレクション型を作成し、ObservableCollection<T>それに応じてインデクサーを変更することができます。唯一の問題は、仮想ではないため、インデクサーをオーバーライドすることはできませんが、代わりにObservableCollection<T>をクラスにラップし、すべての作業を委任して、インデクサーの実装を変更して getter アクセスに追加できることです。

public class PlusOneObservableCollection<T> : IList<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
    private ObservableCollection<T> innerCollection;

    public PlusOneObservableCollection()
    {
        this.innerCollection = new ObservableCollection<T>();
        this.innerCollection.CollectionChanged += InnerCollection_CollectionChanged;
    }
    public PlusOneObservableCollection(IEnumerable<T> collection)
    {
        this.innerCollection = new ObservableCollection<T>(collection);
        this.innerCollection.CollectionChanged += InnerCollection_CollectionChanged;
    }

    private void InnerCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        NotifyCollectionChangedEventHandler handler = this.CollectionChanged;
        if (handler != null)
        {
            handler(this, e);
        }
    }

    public int IndexOf(T item)
    {
        return this.innerCollection.IndexOf(item);
    }

    public void Insert(int index, T item)
    {
        this.innerCollection.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        this.innerCollection.RemoveAt(index);
    }

    public T this[int index]
    {
        get
        {
            //Here is where the actual change takes place
            return this.innerCollection[index + 1];
        }
        set
        {
            this.innerCollection[index] = value;
        }
    }

    public void Add(T item)
    {
        this.innerCollection.Add(item);
    }

    public void Clear()
    {
        this.innerCollection.Clear();
    }

    public bool Contains(T item)
    {
        return this.innerCollection.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        this.innerCollection.CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get { return this.innerCollection.Count; }
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    public bool Remove(T item)
    {
        return this.innerCollection.Remove(item);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return this.innerCollection.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.innerCollection.GetEnumerator();
    }

    public event NotifyCollectionChangedEventHandler CollectionChanged;
    public event PropertyChangedEventHandler PropertyChanged;
}

ただし、このアプローチでは、 から継承するのでObservableCollection<T>はなく、IList<T>これから継承するため、プロパティ タイプを変更する必要があります。ただし、インターフェイスを実装すると、INotifyCollectionChangedユーザーは変更通知をサブスクライブできるようになります。

于 2013-11-03T18:39:56.317 に答える