I have a control ListViewthat displays items from an observable collection. These items must be filtered. I can do this with CollectionViewSource, but the filter needs to be updated every time an item changes.
My items are as follows:
enum Status {Done, Failed, Skipped, ...}
class Project {
public string Name {get;set;}
public Status Status {get;set;}
}
class ProjectViewModel : INotifyPropertyChanged {
private Project project;
public ProjectBuildInfoViewModel(ProjectBuildInfo project)
{
this.project = project;
}
public string Name
{
get { return project.Name; }
set { project.Name = value; OnPropertyChanged("Name"); }
}
}
class CollectionViewModel {
private ObservableCollection<ProjectViewModel> projects =
new ObservableCollection<ProjectViewModel>();
public ObservableCollection<ProjectViewModel> Collection
{
get { return projects; }
private set {projects = value; }
}
}
Then I have this ListViewone whose is ItemSourceattached to the collection.
private CollectionViewModel collection = new CollectionViewModel();
listView.ItemSource = collection.Collection.
It does not filter anything. Therefore, I have these checkboxes and they should indicate which elements (depending on the state) should be displayed. I used then CollectionViewSource:
private void UpdateView()
{
var source = CollectionViewSource.GetDefaultView(collection.Collection);
source.Filter = p => Filter((ProjectViewModel)p);
listStatus.ItemsSource = source;
}
The filter method is as follows:
private bool Filter(ProjectViewModel project)
{
return (ckFilterDone.IsChecked.HasValue && ckFilterDone.IsChecked.Value && project.Status == Status.Done) ||
(ckFilterFailed.IsChecked.HasValue && ckFilterFailed.IsChecked.Value && project.Status == Status.Failed) ||
(ckFilterSkipped.IsChecked.HasValue && ckFilterSkipped.IsChecked.Value && project.Status == Status.Skipped);
}
This has the disadvantage that it captures the values of the flags, so I have to call this method ( UpdateView) every time the flag is checked. But it works.
, "done" , , "", . , , UpdateView. , - . .
, : ?