WPF CollectionView与CollectionViewSource介绍

什么是CollectionView:
CollectionView叫做列表视图,当将列表数据绑定到前端控件时,WPF默认会在两者中间生成一个CollectionView层,该层可以进行排序、分组或过滤等操作。
为什么需要CollectionView:
CollectionView是针对列表数据的一个映像,它不会改变数据,排序、分组等操作针对的是映像数据。这样针对同一组数据,可以同时为用户提供多种展示方式。
CollectionView的具体应用:
当将列表控件的Items直接绑定到数据列表时,此时不会生成CollectionView层;
只有将控件的ItemsSource属性绑定到数据时,才会自动生成CollectionView层;
注意,Items与ItemsSource属性不能同时进行赋值。
什么是CollectionViewSource:
CollectionViewSource是CollectionView的前端代理,由于CollectionView是默认生成的,无法在前端xaml中直接使用,如果希望在AXML将CollectionView绑定到某个列表控件,那么请使用CollectionViewSource. CollectionViewSource拥有一个CollectionView类型的View属性来指定其对应的CollectionView对象,与之对应的,其还有一个Source属性,来指明数据来源.一个简单的流程是:将数据列表绑定到CollectionViewSource的Source属性,然后将列表控件的ItemsSource属性绑定到CollectionViewSource的View属性.为什么不直接将列表控件的ItemSource属性绑定到数据列表呢,这取决于你是否需要查找到该CollectionViewSource进而查找到其View来进行视图操作(比如排序,导航等)

实例地址:WPFCollectionView与CollectionViewSource使用-C#文档类资源-CSDN下载

Viewmodel:

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 = "AAA" });
                Students.Add(new Student() { Name = "李四"+i, Age = 2, Description = "BBB" });
                Students.Add(new Student() { Name = "王五"+i, Age = 3, Description = "CCC" });

            }

            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;
            }
        }
    }

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>


参考链接:https://www.cnblogs.com/zhouyinhui/archive/2007/12/07/987076.html

https://blog.csdn.net/lishuangquan1987/article/details/104692648

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无熵~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值