ICollectionView 过滤器问题 wpf c#

ICollectionView Filter issue wpf c#

我正在尝试过滤 ICollection,但没有应用过滤器,而是它为我提供了所有记录。谓词有什么问题吗?所以有两种方法可以将过滤器应用于集合。首先,它过滤 SW_Version 的记录,然后在第二个过滤器中过滤匹配的 id。基本上我正在实现搜索功能。

   public void Updateswlist()
    {
        CRmappings2 = new ObservableCollection<SPFetchCREntity>(crentities.ToList());
        AllCRSP = CollectionViewSource.GetDefaultView(CRmappings2);
        SearchMU = SelectedSW.SW_Version;
        AllCRSP.Filter = obj =>
        {
            SPFetchCREntity entity = obj as SPFetchCREntity;
            return obj != null && entity.SW_Version == SearchMU.ToString();
        };
        AllCRSP.Refresh();

第二个过滤器

        public void searchMUID()
    {
        AllCRSP.Filter = obj =>
        {
            SPFetchCREntity entity = obj as SPFetchCREntity;
            return obj != null && entity.MU_Identifier == Mupass.ToString();
        };
        AllCRSP.Refresh();
    }

第二个过滤器覆盖第一个。如果您希望能够按这两个属性进行过滤,则需要在谓词中包含这两个条件:

public void searchMUID()
{
    string Mupass = "";
    AllCRSP.Filter = obj =>
    {
        SPFetchCREntity entity = obj as SPFetchCREntity;
        return obj != null && entity.SW_Version == SearchMU.ToString() && entity.MU_Identifier == Mupass.ToString();
    };
    AllCRSP.Refresh();
}

我更喜欢使用 ReactiveUI 及其 derived collection 中的 ReactiveList。它只是工作 - 无需手动触发 Refresh

public class TweetsListViewModel : ReactiveObject
{
    ReactiveList<Tweet> Tweets = new ReactiveList<Tweet>();

    IReactiveDerivedList<TweetTileViewModel> TweetTiles;
    IReactiveDerivedList<TweetTileViewModel> VisibleTiles;

    public TweetsListViewModel()
    {
        TweetTiles = Tweets.CreateDerivedCollection(
            x => new TweetTileViewModel() { Model = x },
            x => true,
            (x, y) => x.CreatedAt.CompareTo(y.CreatedAt));

        VisibleTiles = TweetTiles.CreateDerivedCollection(
            x => x,
            x => !x.IsHidden);
    }
}

如果 SPFetchCREntity 未实现 INotifyPropertyChanged,您可以使用可观察到的重置参数创建派生集合。每次它滴答作响时,整个列表都会被过滤

这就是我解决双过滤器问题的方法。我从这个 link https://code.msdn.microsoft.com/windowsdesktop/CollectionView-Tips-MVVM-d6ebb4a7

得到了灵感
        public void searchMUID()
    {
        Muview = (CollectionView)new CollectionViewSource { Source = CRmappings2 }.View; //CRmappings2 is an observable collection and Muview is a public property of collectionview
        //FirstCRSP = AllCRSP;

        //muview.Filter = null;

        Muview.Filter = obj =>
        {
            SPFetchCREntity entity = obj as SPFetchCREntity;
            return obj != null && entity.SW_Version == SearchMU.ToString() && entity.MU_Identifier == Mupass.ToString();
        };
        Muview.Refresh();
    }