如何调整 ObservableCollection 的大小?

发布于 2024-10-05 00:05:38 字数 1593 浏览 2 评论 0原文

是否可以调整可观察集合的大小或限制集合项目的最大数量?我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莫言歌 2024-10-12 00:05:38

您可以派生 ObservableCollection 类并重写 InsertItem 方法吗?

Can you derive the ObservableCollection class and override the InsertItem method?

眼眸里的那抹悲凉 2024-10-12 00:05:38

试试这个:

public class FixedSizeObservableCollection<T> : ObservableCollection<T>
{
    private readonly int maxSize;
    public FixedSizeObservableCollection(int maxSize)
    {
        this.maxSize = maxSize;
    }

    protected override void InsertItem(int index, T item)
    {
        if (Count == maxSize)
            return; // or throw exception
        base.InsertItem(index, item);
    }
}

try this instead:

public class FixedSizeObservableCollection<T> : ObservableCollection<T>
{
    private readonly int maxSize;
    public FixedSizeObservableCollection(int maxSize)
    {
        this.maxSize = maxSize;
    }

    protected override void InsertItem(int index, T item)
    {
        if (Count == maxSize)
            return; // or throw exception
        base.InsertItem(index, item);
    }
}
病毒体 2024-10-12 00:05:38

您可以推出自己的实现 INotifyCollectionChangedINotifyPropertyChanged 的集合类。您可以使用 ObservableCollection 轻松完成此操作
在您自己的集合类内部。

You can roll your own collection class that implements INotifyCollectionChanged and INotifyPropertyChanged. You can do it fairly easily by using ObservableCollection
internally in your own collection class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文