如何调整 ObservableCollection 的大小?
是否可以调整可观察集合的大小或限制集合项目的最大数量?我有一个 ObservableCollection 作为视图模型中的属性(使用 MVVM 模式)。
视图绑定到集合,我尝试通过提供一个在发生 CollectionChanged
事件时执行的事件处理程序来破解解决方案。在事件处理程序中,我通过根据需要从集合顶部删除尽可能多的项目来修剪集合。
ObservableCollection<string> items = new ObservableCollection<string>();
items.CollectionChanged += new NotifyCollectionChangedEventHandler(Items_Changed);
void Items_Changed(object sender, NotifyCollectionChangedEventArgs e)
{
if(items.Count > 10)
{
int trimCount = items.Count - 10;
for(int i = 0; i < trimCount; i++)
{
items.Remove(items[0]);
}
}
}
此事件处理程序会产生 InvalidOperationException
,因为它不喜欢我在 CollectionChanged
事件期间更改集合这一事实。我应该怎样做才能保持我的收藏规模适当?
解决方案: Simon Mourier 问我是否可以创建一个从 ObservableCollection
派生的新集合并覆盖 InsertItem()
,这正是我为获得自动调整大小的 ObservableCollection 类型所做的事情。
public class MyCollection<T> : ObservableCollection<T>
{
public int MaxCollectionSize { get; set; }
public MyCollection(int maxCollectionSize = 0) : base()
{
MaxCollectionSize = maxCollectionsize;
}
protected override void InsertItem(int index, T item)
{
base.InsertItem(index, item);
if(MaxCollectionSize > 0 && MaxCollectionSize < Count)
{
int trimCount = Count - MaxCollectionSize;
for(int i=0; i<trimCount; i++)
{
RemoveAt(0);
}
}
}
}
Is it possible to resize an Observable Collection or perhaps restrict the max amount of collection items? I have an ObservableCollection as a property in a View Model (using the MVVM pattern).
The view binds to the collection and I've attempted to hack a solution by providing an event handler that executes when a CollectionChanged
event occurs. In the event handler, I trimmed the collection by removing as many items off the top of the collection as necessary.
ObservableCollection<string> items = new ObservableCollection<string>();
items.CollectionChanged += new NotifyCollectionChangedEventHandler(Items_Changed);
void Items_Changed(object sender, NotifyCollectionChangedEventArgs e)
{
if(items.Count > 10)
{
int trimCount = items.Count - 10;
for(int i = 0; i < trimCount; i++)
{
items.Remove(items[0]);
}
}
}
This event handler yields an InvalidOperationException
because it doesn't like the fact that I alter the collection during a CollectionChanged
event. What should I do to keep my collection sized appropriately?
Solution:
Simon Mourier asked if I could create a new collection derived from ObservableCollection<T>
and override InsertItem()
and thats just what I did to have an auto-resizing ObservableCollection type.
public class MyCollection<T> : ObservableCollection<T>
{
public int MaxCollectionSize { get; set; }
public MyCollection(int maxCollectionSize = 0) : base()
{
MaxCollectionSize = maxCollectionsize;
}
protected override void InsertItem(int index, T item)
{
base.InsertItem(index, item);
if(MaxCollectionSize > 0 && MaxCollectionSize < Count)
{
int trimCount = Count - MaxCollectionSize;
for(int i=0; i<trimCount; i++)
{
RemoveAt(0);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以派生 ObservableCollection 类并重写 InsertItem 方法吗?
Can you derive the ObservableCollection class and override the InsertItem method?
试试这个:
try this instead:
您可以推出自己的实现
INotifyCollectionChanged
和INotifyPropertyChanged
的集合类。您可以使用ObservableCollection
轻松完成此操作在您自己的集合类内部。
You can roll your own collection class that implements
INotifyCollectionChanged
andINotifyPropertyChanged
. You can do it fairly easily by usingObservableCollection
internally in your own collection class.