WPF ListCollectionView实现过滤功能

1 篇文章 0 订阅

关于ICollectionView/CollectionView/BindingListCollectionView/ItemCollection/ListCollectionView的介绍:
https://www.cnblogs.com/tianciliangen/p/7010103.html

它们之间的关系是:
在这里插入图片描述
之前我写WPF实现过滤功能是从DataSource进行过滤,实现界面变化,这样效率比较低。搞清楚了下面的关系,就好些代码了:
在这里插入图片描述
现在讲讲从ListCollecitonView视图进行过滤的使用:

MainWindowViewModel.cs

public class MainWindowViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private List<Student> students = new List<Student>();
        public List<Student> Students
        {
            get { return students; }
            set { students = value; }
        }

        private string nameFilterStr;

        public string NameFilterStr
        {
            get { return nameFilterStr; }
            set
            {
                nameFilterStr = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("NameFilterStr"));
                StudentListView.Refresh();
            }
        }

        private string desFilterStr;

        public string DesFilterStr
        {
            get { return desFilterStr; }
            set
            {
                desFilterStr = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DesFilterStr"));

                StudentListView.Refresh();
            }
        }


        public ListCollectionView StudentListView { get; set; }
        public MainWindowViewModel()
        {
            StudentListView = new ListCollectionView(Students);
            for (int i = 0; i < 100000; i++)
            {
                Students.Add(new Student() { Name = "张三"+i, Age = 1, Description = "fsfdsafds" });
                Students.Add(new Student() { Name = "李四"+i, Age = 2, Description = "及王鹏飞" });
                Students.Add(new Student() { Name = "王五"+i, Age = 3, Description = "都没法鲁大师南方男" });
                Students.Add(new Student() { Name = "刘6"+i, Age = 4, Description = "副书记丁阿基佛尔" });
                Students.Add(new Student() { Name = "鲁7"+i, Age = 5, Description = "飞达拉斯科技佛电视剧" });
            }

            StudentListView.Filter = Filter;
        }
        private bool Filter(object obj)
        {
            Student s = obj as Student;
            if (s == null) return false;

            if (string.IsNullOrEmpty(NameFilterStr) && string.IsNullOrEmpty(DesFilterStr))
            {
                return true;
            }
            else if (!string.IsNullOrEmpty(NameFilterStr) && string.IsNullOrEmpty(DesFilterStr))
            {
                if (s.Name.Contains(NameFilterStr))
                {
                    return true;
                }
                return false;
            }
            else if (string.IsNullOrEmpty(NameFilterStr) && !string.IsNullOrEmpty(DesFilterStr))
            {
                if (s.Description.Contains(DesFilterStr))
                {
                    return true;
                }
                return false;
            }
            else
            {
                if (s.Name.Contains(NameFilterStr)&&s.Description.Contains(DesFilterStr))
                {
                    return true;
                }
                return false;
            }
        }
    }

MainWindow.xaml

<Window x:Class="ListCollectionView实现过滤功能.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ListCollectionView实现过滤功能"
        xmlns:vm="clr-namespace:ListCollectionView实现过滤功能.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <vm:MainWindowViewModel></vm:MainWindowViewModel>
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Margin="2">
            <Label>名称过滤:</Label>
            <TextBox Width="100" Text="{Binding NameFilterStr,Mode=TwoWay, UpdateSourceTrigger=LostFocus}"></TextBox>
            <Label>描述过滤:</Label>
            <TextBox Width="100" Text="{Binding DesFilterStr,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        </StackPanel>
        <DataGrid ItemsSource="{Binding StudentListView}" Grid.Row="1"></DataGrid>
    </Grid>
</Window>

在这里插入图片描述
在这里插入图片描述
ListCollectionView过滤,就是调用Filter,然后告诉视图,每个元素是否显示就可以了,最后别忘记StudentListView.Refresh();

另外一种方式

使用CollectionViewSource

stepModeSourceview =CollectionViewSource.GetDefaultView(StepModeSource);
stepModeSourceview.Filter = FileterStepModeSource;
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值