2

Clientのサブクラスである というクラスがありますConfigurable

ObservableCollection<Client>として表示する必要がある がありますObservableCollection<Configurable>。これにより、一般的なレイアウト生成コードからリストにデータバインドできます。また、リストをクリアし、リストにアイテムを追加できるようにする必要があります。もちろん、リストにアイテムを追加するときは、実行時のタイプ チェックを実行して、Configurable追加される一般的なアイテム ( ) が適切なタイプ ( Client) であることを確認する必要があります。

のようなクラスを想像していObservableSurrogateCollection<T>ます。 T一般クラス ( Configurable) です。を渡して構築します。ObservableCollection<T2>ここで、T2は のサブクラスですT。それにデータバインドでき、ラップされたリストのすべてのコレクション変更イベントが正しくルーティングされます (双方向)。

これは存在しますか?これは私がすべきことではありませんか?.NET 4.0 が言語レベルでそのような機能をサポートすることを読んだと思いますか?

私はこれらのオプションを見てきました:

  • ReadOnlyObservableCollection<T>. これは本当に近いです。ただし、読み取り専用であるため、アイテムを追加または削除することはできません。
  • 非ジェネリックObservableCollection. 存在する場合、これを見つけることができないようです。

助けてくれてありがとう!

4

1 に答える 1

2

いいえ、それは私の知る限り存在しませんが、実装するのは難しくありません。もちろん、分散のニュアンスが含まれているため、コレクションは、割り当て/追加時にキャストの問題を予期する必要があります。配列( T[])は既にこのように動作することに注意してください(参照型の配列にはバリアンス サポートがあり、型キャスト チェックなどがあります) が、配列に追加することはできず、変更イベントは発生しません。

これを実装するには、大部分がカプセル化作業になります。オブジェクトの構築 (新しい行へのデータ バインディングの場合) はややこしい作業であり、追加のインターフェイスを追加する必要がある場合があります。

4.0 はここに何も追加しません: What C# 4.0 covariance *doesn't* do

テストされていませんが、次のとおりです。

public class ObservableCollection<TBase, TActual> : IList<TBase>, IBindingList, INotifyCollectionChanged
    where TBase : class
    where TActual : class, TBase
{

    private readonly ObservableCollection<TActual> innerList;

    public ObservableCollection()
        : this((IEnumerable<TActual>)null) {}
    public ObservableCollection(IEnumerable<TBase> data)
        : this(data == null ? null : data.Cast<TActual>()) {}
    public ObservableCollection(IEnumerable<TActual> data)
    {
        innerList = data == null ? new ObservableCollection<TActual>()
            : new ObservableCollection<TActual>(data);
        innerList.CollectionChanged += new NotifyCollectionChangedEventHandler(innerList_CollectionChanged);
    }

    void innerList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        ListChangedEventHandler handler = ListChanged;
        if(handler != null) {
            ListChangedEventArgs args = null;
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    args = new ListChangedEventArgs(ListChangedType.ItemAdded, e.NewStartingIndex);
                    break;
                case NotifyCollectionChangedAction.Remove:
                    args = new ListChangedEventArgs(ListChangedType.ItemDeleted, e.OldStartingIndex);
                    break;
                case NotifyCollectionChangedAction.Reset:
                    args = new ListChangedEventArgs(ListChangedType.Reset, -1);
                    break;
                case NotifyCollectionChangedAction.Replace:
                    args = new ListChangedEventArgs(ListChangedType.ItemChanged, e.NewStartingIndex);
                    break;
                case NotifyCollectionChangedAction.Move:
                    args = new ListChangedEventArgs(ListChangedType.ItemMoved, e.NewStartingIndex, e.OldStartingIndex);
                    break;
            }
            if(args != null) handler(this, args);
        }
        NotifyCollectionChangedEventHandler handler2 = CollectionChanged;
        if (handler2 != null) handler2(this, e);
    }

    public void AddIndex(PropertyDescriptor property) {}

    public object AddNew()
    {
        TActual obj = (TActual)Activator.CreateInstance(typeof(TActual));
        Add(obj);
        return obj;
    }

    public bool AllowEdit { get { return !IsReadOnly; } }
    public bool AllowNew { get { return !IsFixedSize; } }
    public bool AllowRemove { get { return !IsFixedSize; } }

    public void ApplySort(PropertyDescriptor property, ListSortDirection direction)
    {
        throw new System.NotImplementedException();
    }

    public int Find(PropertyDescriptor property, object key)
    {
        throw new System.NotImplementedException();
    }

    public bool IsSorted { get { return false; } }

    public event ListChangedEventHandler ListChanged;
    public event NotifyCollectionChangedEventHandler CollectionChanged;

    public void RemoveIndex(PropertyDescriptor property) { }

    public void RemoveSort()
    {
        throw new System.NotImplementedException();
    }

    public ListSortDirection SortDirection
    {
        get { return ListSortDirection.Ascending; }
    }

    public PropertyDescriptor SortProperty
    {
        get { throw new System.NotImplementedException(); }
    }

    public bool SupportsChangeNotification { get { return true; } }
    public bool SupportsSearching { get { return false; } }
    public bool SupportsSorting { get { return false; } }

    int IList.Add(object value)
    {
        int index = innerList.Count;
        Add((TBase)value);
        return index;
    }
    public void Add(TBase value)
    {
        innerList.Add((TActual)value);
    }

    public void Clear()
    {
        innerList.Clear();
    }

    bool IList.Contains(object value)
    {
        return Contains((TBase)value);
    }
    public bool Contains(TBase value)
    {
        return innerList.Contains((TActual)value);
    }

    int IList.IndexOf(object value)
    {
        return IndexOf((TBase)value);
    }
    public int IndexOf(TBase value)
    {
        return innerList.IndexOf((TActual)value);
    }

    void IList.Insert(int index, object value)
    {
        Insert(index, (TBase)value);
    }
    public void Insert(int index, TBase value)
    {
        innerList.Insert(index, (TActual)value);
    }

    public bool IsFixedSize
    {
        get { return ((IList)innerList).IsFixedSize; }
    }

    public bool IsReadOnly
    {
        get { return ((IList)innerList).IsReadOnly; }
    }

    void IList.Remove(object value)
    {
        Remove((TBase)value);
    }
    public bool Remove(TBase value)
    {
        return innerList.Remove((TActual)value);
    }

    public void RemoveAt(int index)
    {
        innerList.RemoveAt(index);
    }

    object IList.this[int index]
    {
        get { return innerList[index]; }
        set { innerList[index] = (TActual)value; }
    }
    public TBase this[int index]
    {
        get { return innerList[index]; }
        set { innerList[index] = (TActual)value; }
    }

    void ICollection.CopyTo(System.Array array, int index)
    {
        ((IList)innerList).CopyTo(array, index);
    }
    public void CopyTo(TBase[] array, int index)
    {
        innerList.CopyTo((TActual[])array, index);
    }

    public int Count { get { return innerList.Count; } }

    public bool IsSynchronized
    {
        get { return ((IList)innerList).IsSynchronized; }
    }

    public object SyncRoot
    {
        get { return ((IList)innerList).SyncRoot; }
    }

    public IEnumerator<TBase> GetEnumerator()
    {
        return innerList.Cast<TBase>().GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return innerList.GetEnumerator();
    }
}
于 2009-12-08T19:58:21.973 に答える