我正在尝试实现一个可绑定的集合-一个专用的堆栈-它需要与我对其进行的任何更新一起显示在Windows 8应用程序的一页上。为此,我实现了INotifyCollectionChanged和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 implementationpublic event NotifyCollectionChangedEventHandler CollectionChanged;#endregionpublic 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();}该集合类用于定义页面拥有的基础类实例的属性,该属性设置为其DataContext(页面的Calculator属性),然后绑定到GridView:<GridView x:Name="StackGrid" ItemsSource="{Binding Stack, Mode=OneWay}" ItemContainerStyle="{StaticResource StackTileStyle}" SelectionMode="None">... ItemTemplate omitted for length ...绑定最初在页面导航至时有效-堆栈中的现有项目显示得很好,但是添加到堆栈中或从堆栈中删除的项目不会反映在GridView中,直到页面被导航离开和返回。调试显示,堆栈中的CollectionChanged事件始终为null,因此从不会在更新时调用它。我想念什么? 最佳答案 刚才,我想与可绑定的自定义集合面临相同的问题。我发现只能绑定派生自Collection<>的类。为什么?现在我还不知道。因此,如果您真的希望它起作用,则派生Collection<>表格,但这会与您的设计混淆。关于data-binding - INotifyCollectionChanged是否不足以更新数据绑定(bind)控件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15044458/
10-17 00:23