In WinRT, a collection that implements INotifyCollectionChanged without updating the ItemsControl
I am writing my own collection class that also implements INotifyCollectionChanged. I am using it in a Windows 8 storage app (winRT). I wrote a unit test that proves that changing the contents of a list causes all matching events to fire, with the same events as a "normal" observable collection. However, when I bind the ItemsSource property of the ItemsControl (I tried GridView, ListView and even a simple vanilla ItemsControl) to a collection, it doesn't affect the UI when the collection changes.
Does the base HAVE collection type of ObservableCollection use it to work, or can I write my own collection class?
Thnx
source to share
You can use ICollectionView
which also has advanced filtering functionality. If you want to create a class, consider the one available in the Code Project .
In particular, I noticed that the UI subscribes to VectorChanged
, so you should be fine if only implemented IObservableCollection
earlier in the comments.
The event VectorChanged
takes on an interface of the type IVectorChangedEventArgs
and I didn't find any specific classes when browsing. It's not difficult to create. Here's one that can be instantiated in a similar way to how you instantiate NotifyPropertyChangedEventArgs
. It is private as it is 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; }
}
source to share