繁体   English   中英

如何使用 CollectionViewSource 过滤 ObservableCollection<T> 显示在列表视图中

[英]How to use CollectionViewSource to filter ObservableCollection<T> displayed in ListView

我见过很多类似的问题,但我找不到问题/答案或教程,其中清楚地列出了使其工作所需的所有组件。 我正在尝试遵循 MVVM,但这完全是一个 UI 问题,我不反对做一些代码隐藏。

我正在努力实现的目标:

  • ListView.ItemsSource绑定到ObservableCollection<T>
  • 基于TextBox过滤ListView显示的项目
  • 过滤器随着用户在TextBox类型而更新

在我的ViewModel我有这样的东西:

private ObservableCollection<Customer> _customers;
public ObservableCollection<Customer> Customers
{
    get { return _customers; }
    set
    {
        _customers= value;
        RaisePropertyChanged("Customers");
    }
}

private Customer _selected_Customer;
public Customer Selected_Customer
{
    get { return _selected_Customer; }
    set
    {
        _selected_Customer= value;
        RaisePropertyChanged("Selected_Customer");
    }
}

private string _filtered_Name;
public string Filtered_Name
{
    get { return _filtered_Name; }
    set
    {
        _filtered_Name = value;
        RaisePropertyChanged("Filtered_Name");
    }
}

在我的 XAML 中,它是这样的:

<CollectionViewSource x:Key="cvs"
                      x:Name="Customer_Details_View"
                      Source="{Binding Path=Customers}"/>

<TextBox x:Name="Filtered_Name" Text="{Binding Filtered_Name, Mode=TwoWay}"/>

<ListView ItemsSource="{Binding ElementName=Customer_Details_View}"
          SelectedItem="{Binding Selected_Customer, Mode=TwoWay}">

我想用以下逻辑过滤我的ObservableCollection<Customer>Customer.Name.ToLower().Contains(Filtered_Name.ToLower())

如何将TextBox.Text绑定到CollectionViewSource或利用CollectionViewSource.Filter事件来应用上述过滤器?

您可能希望在 VM 中声明 collectionviewsource 并直接绑定到它。 完成后,您可以更改过滤器,如此问题CollectionViewSource, how to filter data? 中所回答的那样

VM 示例,如果没有按摩可能无法工作。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace CollectionViewSourceExample
{
    class MainWindowViewModel : INotifyPropertyChanged
    {
        private string _filter;

        public MainWindowViewModel()
        {
            SourceCollection = new ObservableCollection<string>();
            SourceCollection.Add("Fred Flintstone");
            SourceCollection.Add("Wilma Flintstone");
            SourceCollection.Add("Bambam Flintstone");
            SourceCollection.Add("Barny Rubble");
            SourceCollection.Add("Betty Rubble");

            ViewSource = (ListCollectionView)CollectionViewSource.GetDefaultView(SourceCollection);
            ViewSource.Filter = SourceFilter;
            ViewSource.IsLiveFiltering = true;
        }

        private bool SourceFilter(object obj)
        {
            string val = (string)obj;
            return string.IsNullOrWhiteSpace(Filter) || val.Contains(Filter);
        }

        public string Filter
        {
            get { return _filter; }
            set
            {
                _filter = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Filter"));
                ViewSource.Refresh();
            }
        }

        public ObservableCollection<string> SourceCollection { get; }
        public ListCollectionView ViewSource { get; }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

暂无
暂无

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

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