本文介绍了removeall过的ObservableCollections?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找的Linq的方式(如RemoveAll方法的列表),它可以删除选定的从我的ObservableCollection项目。

I am looking for Linq way (like RemoveAll method for List) which can remove selected items from my ObservableCollection.

我太新创建的扩展方法我。有什么办法,我从的ObservableCollection删除项目传递一个Lambda表达式?

I am too new to create an extension method for myself. Is there any way I remove items from ObservableCollection passing a Lambda expression?

推荐答案

我不知道的方式只有删除选择的项目。但是创建一个扩展方法是直截了当:

I am not aware of a way to remove only the selected items. But creating an extension method is straight forward:

public static class ExtensionMethods
{
    public static int Remove<T>(
        this ObservableCollection<T> coll, Func<T, bool> condition)
    {
        var itemsToRemove = coll.Where(condition).ToList();

        foreach (var itemToRemove in itemsToRemove)
        {
            coll.Remove(itemToRemove);
        }

        return itemsToRemove.Count;
    }
}

这将删除的ObservableCollection 符合条件。你可以把它像:

This removes all items from the ObservableCollection that match the condition. You can call it like that:

var c = new ObservableCollection<SelectableItem>();
c.Remove(x => x.IsSelected);

这篇关于removeall过的ObservableCollections?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 01:57