3

INotifyCollectionChanged も実装する独自のコレクション クラスを作成しています。Windows 8 ストア アプリケーション (winRT) で使用しています。リストの内容を変更すると、「通常の」監視可能なコレクションが発生させるのと同じイベントで、すべての適切なイベントが発生することを証明する単体テストを作成しました。それでも、ItemsControl の ItemsSource プロパティをコレクションにバインドすると (GridView、ListView、さらには普通の ItemsControl も試しました)、コレクションが変更されても UI には影響しません。

基礎となるコレクション型は、それが機能するために ObservableCollection である必要がありますか、それとも独自のコレクション クラスを作成することは可能ですか?

thnx

4

1 に答える 1

0

ICollectionViewフィルタリング用の拡張機能を備えた を使用することもできます。既製のクラスが必要な場合は、 Code Project で入手できるものをチェックしてください。

特に、UI がイベントにサブスクライブしていることに気付きました。そのため、コメントで前述したVectorChanged実装のみを行うのが適切なはずです。IObservableCollection

VectorChangedイベントは type のインターフェースを取り、周りIVectorChangedEventArgsを見回しても具象クラスは見つかりませんでした。ただし、作成するのは難しくありません。のインスタンスを作成する方法と同様に作成できるものを次に示しますNotifyPropertyChangedEventArgs。コレクションクラスでのみ使用されるため、プライベートです。

private sealed class VectorChangedEventArgs : IVectorChangedEventArgs
{
    public VectorChangedEventArgs(NotifyCollectionChangedAction action, object item, int index)
    {
        switch (action)
        {
            case NotifyCollectionChangedAction.Add:
            CollectionChange = CollectionChange.ItemInserted;
            break;
            case NotifyCollectionChangedAction.Remove:
            CollectionChange = CollectionChange.ItemRemoved;
            break;
            case NotifyCollectionChangedAction.Move:
            case NotifyCollectionChangedAction.Replace:
            CollectionChange = CollectionChange.ItemChanged;
            break;
            case NotifyCollectionChangedAction.Reset:
            CollectionChange = CollectionChange.Reset;
            break;
            default:
            throw new ArgumentOutOfRangeException("action");
        }
        Index = (uint)index;
        Item = item;
    }

    /// <summary>
    /// Gets the affected item.
    /// </summary>
    public object Item { get; private set; }

    /// <summary>
    /// Gets the type of change that occurred in the vector.
    /// </summary>
    public CollectionChange CollectionChange { get; private set; }

    /// <summary>
    /// Gets the position where the change occurred in the vector.
    /// </summary>
    public uint Index { get; private set; }
}
于 2013-05-06T15:13:25.077 に答える