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