I'm trying to implement a bindable collection - a specialized stack - which needs to be displayed on one page of my Windows 8 app along with any updates made to it as they happen. For this, I've implemented INotifyCollectionChanged and IEnumerable<>:
public class Stack : INotifyCollectionChanged, IEnumerable<Number>
{
...
public void Push(Number push)
{
lock (this)
{
this.impl.Add(push);
}
if (this.CollectionChanged != null)
this.CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, push));
}
...and the equivalents for other methods...
#region INotifyCollectionChanged implementation
public event NotifyCollectionChangedEventHandler CollectionChanged;
#endregion
public IEnumerator<Number> GetEnumerator()
{
List<Number> copy;
lock (this)
{
copy = new List<Number>(impl);
}
copy.Reverse();
foreach (Number num in copy)
{
yield return num;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
This collection class is used to define a property of an underlying class instance owned by the page, which is set as its DataContext (the Calculator property of the Page), and is then bound to a GridView:
<GridView x:Name="StackGrid" ItemsSource="{Binding Stack, Mode=OneWay}" ItemContainerStyle="{StaticResource StackTileStyle}" SelectionMode="None">
... ItemTemplate omitted for length ...
The binding works initially when the page is navigated to - existing items in the Stack are displayed just fine, but items added to/removed from the Stack are not reflected in the GridView until the page is navigated away from and back to. Debugging reveals that the CollectionChanged event in the Stack is always null, and thus it never gets called on update.
What am I missing?