3

I am writing my own collection class that also implements INotifyCollectionChanged. I am using it in a windows 8 store application (winRT). I wrote a unit test that proves that modifying the content of the list raises all the proper events with the same events a "normal" observable collection would raise. Still, when I bind the ItemsSource property of an ItemsControl (I tried GridView, ListView, and even plain vanilla ItemsControl) to the collection, it does not affect the UI when the collection is changed.

Does the underlying collection type HAVE to be an ObservableCollection for it to work or is it possible to write my own collection class?

Thnx

Kobi Hari
  • 1,259
  • 1
  • 10
  • 25
  • http://msdn.microsoft.com/en-us/library/windows/apps/br226052.aspx – SLaks Jan 29 '13 at 16:54
  • I noticed the IObservableVector interface but it appears that ObservableCollection does not implement it (or am I wrong). Is it now the only "observable collection interface" that needs to be implemented? – Kobi Hari Jan 29 '13 at 16:57
  • Perhaps `ObservableCollection` silently gets projected to `IObservableVector`... – Filip Skakun Jan 29 '13 at 18:59

1 Answers1

0

You can use an ICollectionView that has some extended functionality for filtering as well. If you want a premade class, check out one available at Code Project.

In particular, I noticed that the UI subscribes to the VectorChanged event, so you should be good with only implementing IObservableCollection noted earlier in the comments.

The VectorChanged event takes an interface of type IVectorChangedEventArgs, and I found no concrete classes when looking around. It's not difficult to create one though. Here's one that can be created similar to how you create an instance of NotifyPropertyChangedEventArgs. It's private since it's only used in the collection class.

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; }
}
Patrick
  • 17,669
  • 6
  • 70
  • 85