WPF ListCollectionView實現過濾功能

關於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();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章