简体   繁体   English

我不能在 ObservableCollection 上使用 Filter 方法

[英]I can't use method Filter on ObservableCollection

On my viewModel I developed this code that will allow me to implement a research filter:在我的viewModel我开发了这段代码,它将允许我实现一个研究过滤器:

        public ICollection<Nc> Ncs
        {
            get
            {
                var ncs = _ncManager.Ncs;

                _ncCollectionView = CollectionViewSource.GetDefaultView(ncs);
                _ncCollectionView.Filter = _DoesNcMatchFileNameFilter;

                return ncs;
            }
        }
        private ICollectionView _ncCollectionView;

This method works, but if I try to implement another similar method in another viewModel :此方法有效,但如果我尝试在另一个viewModel实现另一个类似的方法:

    private ObservableCollection<Material> _materials;
    public ObservableCollection<Material> materials
    {
       get
       {
          var materials = Material.Name;
          _materialCollectionView = (CollectionViewSource)CollectionViewSource.GetDefaultView(materials);
          _materialCollectionView.Filter = _DoesMaterialMatchFileNameFilter;
    
          return Materials;
       }
    }
    private CollectionViewSource _materialCollectionView;

I get an error: CollectionViewSrouce.Filter can only appear on the left hand side of += or -=".我收到一个错误: CollectionViewSrouce.Filter can only appear on the left hand side of += or -=".

Why can't I do the same thing?为什么我不能做同样的事情? What did I do wrong?我做错了什么?

I've tried to read this page ObservableCollection<> vs. List<> but it didn't help me.我试图阅读此页面ObservableCollection<> vs. List<>但它没有帮助我。

should _materialCollectionView not be of type ICollectionView ? _materialCollectionView不应该是ICollectionView类型吗?

So:所以:

private ObservableCollection<Material> _materials;
    public ObservableCollection<Material> materials
    {
       get
       {
          var materials = Material.Name;
          _materialCollectionView = CollectionViewSource.GetDefaultView(materials);
          _materialCollectionView.Filter = _DoesMaterialMatchFileNameFilter;
    
          return Materials;
       }
    }
    private ICollectionView _materialCollectionView;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM