WPF CollectionView与CollectionViewSource介绍

祝嘉懿
2023-12-01

什么是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

 类似资料: