Filtering allows a tree to be narrowed down to only display items that match filter criteria. A powerful data filtering mechanism is provided that uses string, boolean, and predicate-based logic to filter items. The underlaying data model is not altered in any way, only the view is.
Using Filters
To activate filtering functionality, set the Treetrue
. It is false
by default.
When filtering is active, the control will iterate over every item in the tree and will call the Tree
Included
- The item passed all filter conditions and is included in the filter. Tree descendants will also be examined by the filter.IncludedWithDescendants
- The item passed all filter conditions and is included in the filter. Tree descendants may also be examined by the filter, but only to determine if their ancestors require expansion. Tree descendants will be included in the filter regardless of their own filter result.IncludedByDescendants
- The item did not pass all filter conditions and will only be included in the filter if one or more of its tree descendants are included. Tree descendants will also be examined by the filter.Excluded
- The item did not pass all filter conditions and will not be included in the filter. None of its tree descendants will be examined by the filter.
Important
When a filter includes an item, that item will be visible in the tree. When a filter excludes an item, that item will not be visible in the tree.
The Filter method is virtual and at a low level can be overridden to use custom filter logic.
However, this generally is not necessary since there is a higher level filtering mechanism that is provided via the TreeDataFilterResult.Included
.
The main benefit of this approach is that there are several built-in IData
Another benefit of using IDatafalse
to prevent that filter from being applied. This is especially useful when building complex filter groups, as described below. You still want to make sure the Treefalse
when no filtering is needed, since that will improve performance.
Tip
It is most efficient to set and configure the Datatrue
.
The Tree
Automatically Including Descendants
The DataDataFilterResult.Included
but can alternatively be set to DataFilterResult.IncludedWithDescendants
if you wish for all descendant items automatically be included without needing to pass the filter themselves.
All of the built-in filters return the Included
Predicate Filters
The PredicatePredicate<object>
to return filter results.
Simply set the Predicate property to the predicate to use. If the predicate returns true
, the IncludedDataFilterResult.IncludedByDescendants
will be returned.
This example code shows how to set a predicate filter that assumes each item is a TreeNodeModel
(your custom model type) and checks to see if the Name
starts with "Foo"
.
String Filters
The abstract String
Target Value
The Value property needs to be set to the operand value used by the filter. The is the target value against which the comparison will be made.
Operation
The Operation property is set to a StringContains
, but all of these operations are available: Contains
, NotContains
, StartsWith
, NotStartsWith
, EndsWith
, NotEndsWith
, Equals
, NotEquals
, RegexMatch
, and NotRegexMatch
.
Culture and Case Sensitivity
The StringStringComparison
value that determines how to compare the strings in terms of culture and case sensitivity. The default value is CurrentCultureIgnoreCase
but can be set to other options if you would prefer ordinal or case sensitive comparisons.
Regular Expressions
When Operation is set to RegexMatch
or NotRegexMatch
, regular expression matching will be used. The regular expression should be specified in the Value property. If a String
If any other RegexOptions
(matching options) need to be set, they can be assigned to the Regex
Implementing a String Filter
This example code shows how to create a TreeNodeModelStringFilter
class that knows how to examine a TreeNodeModel
(your custom model type) and check its Name
property.
Once the string filter has been created, it can be implemented in XAML like this to filter all models whose Name
starts with "Foo"
:
The filter's value or other properties can be dynamically updated to refresh the filter results. For instance, with string filters it is common for a TextBox
in the user interface to update a string filter's value as text is typed.
Boolean Filters
The abstract Boolean
Target Value
The Value property needs to be set to the operand value used by the filter. The is the target value against which the comparison will be made.
Operation
The Operation property is set to a BooleanEquals
, but all of these operations are available: Equals
, NotEquals
, And
, Or
and Xor
.
Implementing a Boolean Filter
This example code shows how to create a TreeNodeModelBooleanFilter
class that knows how to examine a TreeNodeModel
(your custom model type) and check its IsActive
property.
Once the boolean filter has been created, it can be implemented in XAML like this to filter all models whose IsActive
property is true
:
The filter's value or other properties can be dynamically updated to refresh the filter results. For instance, with boolean filters it is common for a CheckBox
in the user interface to update a boolean filter's value as its checked state changes.
Filter Groups
The Data
Note
A filter group can contain other filters or filter groups. Combine that with the ability for filters to be individually enabled or disabled (via IData
Child Filters
The Children property is the collection of child filters to use.
Operation
The Operation property is set to a DataAnd
(all child filters must pass), but Or
(any child filter must pass) can be chosen as well.
Implementing a Filter Group
This example code shows how to use the filters described above in a filter group, where both must pass for an item to be included:
Custom Filters
While the built-in classes described above will suit most needs, custom IData
Your custom filter can then be set to the Tree
It is also possible to inherit the other base classes like String
Auto-Expanding to Filtered Items
The Treetrue
to ensure that when filtering occurs and an item is included in the filter, all of its ancestor items will be expanded automatically. The property's default value is false
, meaning don't enable this behavior.
This feature is useful when you want to ensure that filtered items are always fully visible immediately and can't be hidden by collapsed ancestor items.
When this feature is enabled, as soon as filtering is deactivated, the expansion state of the items is restored to what it was before filtering activated.
Empty Content
The TreeDataTemplate
that should be displayed in the control whenever there are no visible items. If additional data needs to be passed into the data template, set it to the Empty
This feature is useful when filters can possibly hide all items and a message should be displayed in their place.