WPF - Filtering / Finding Multiple Collection Views in a Tree
I have a tree associated with a collection and each item in the collection is bound to a different collection. (using hierachle data templates)
I would like to use the .Filter collection handler to search through a tree. The problem is that I need multiple kinds of collections.
What would be the best way to filter tree items like a search word? I can do this with a single collection binding, but when there are collections within collections, I have problems.
source to share
The easiest way I have found this is to create a SearchFilter property
public string SearchFilter
{
get { return _searchFilter; }
set
{
_searchFilter = value;
OnPropertyChanged("MyTreeViewBoundCollection");
}
}
You bind a search filter to the text box and every time the search text box changes, you notify that the collection has changed
<TextBox Text="{Binding Path=TemplateDataSchema.SearchFilter, UpdateSourceTrigger=PropertyChanged}"/>
Once the change has occurred in the SearchFilter, the WPF binding system will request a Collection Property which can then be filtered
public ObservableCollection<Category> MyTreeViewBoundCollection
{
get {
if (_searchFilter.Trim().Length < 1)
return myObject.Categories;
else
{
ObservableCollection<Category> cats = new ObservableCollection<Category>();
string searchText = _searchFilter.ToLower().Trim();
foreach (Category cat in myObject.Categories)
{
Category tmpCat = new Category(cat.CategoryName);
foreach (Field field in cat.Fields)
{
if (field.DataDisplayName.ToLower().Contains(searchText))
tmpCat.Fields.Add(field);
}
if (tmpCat.Fields.Count > 0)
cats.Add(tmpCat);
}
return cats;
}
}
}
This will return the filter collection.
source to share