.NET MAUI CollectionView Filtering
The CollectionView provides the functionality to programmatically filter its data at runtime.
You can achieve this by adding different filter descriptors in the RadCollectionView.FilterDescriptors
collection.
The following descriptor types are supported:
- Text Filter Descriptor
- Numerical Filter Descriptor
- DateTime Filter Descriptor
- TimeSpan Filter Descriptor
- Boolean Filter Descriptor
- Nested Property Text Filter Descriptor
- Distinct Values Filter Descriptor
- Composite Filter Descriptor
- Delegate Filter Descriptor
All FilterDescriptors
are located in the Telerik.Maui.Controls.Data
namespace.
Text Filter Descriptor
The TextFilterDescriptor
represents a descriptor which filters by properties of the string
data type.
The descriptor supports the following properties:
-
PropertyName
—Defines the name of the property that is used to retrieve the filter value. -
Operator
—Defines theTextOperator
value that defines how theValue
member is compared with each value from the items source. -
Value
—Defines the value used in the comparisons. This is the right operand of the comparison. -
IsCaseSensitive
—Defines a value that determines whether the text comparisons will be case-sensitive. The default value istrue
.
Example with Text Filter Descriptor
The following example implements a TextFilterDescriptor
and uses a RadEntry
control that allows the user to enter a string of text to filter the CollectionView data.
1. Define the RadCollectionView
and RadEntry
in XAML:
2. Add the telerik
namespace:
3. Create a sample DataModel
:
4. Define the ViewModel
class:
5. Add the TextFilterDescriptor
to the RadCollectionView
inside the RadEntry
TextChanged
event:
This is the result:
For a runnable demo with the CollectionView Filtering example, see the SDKBrowser Demo Application and go to the CollectionView > Filtering category.
Numerical Filter Descriptor
The NumericalFilterDescriptor
represents a descriptor which filters by properties of the numerical
data type.
The descriptor exposes the following properties:
-
PropertyName
—Defines the name of the property that is used to retrieve the filter value. -
Value
—Defines the value used in the comparisons. This is the right operand of the comparison. -
Operator
—Defines theNumericalOperator
value that defines the boolean logic behind the left and right operand comparison.
DateTime Filter Descriptor
The DateTimeFilterDescriptor
is a descriptor which filters by properties of the System.DateTime
data type.
The descriptor exposes the following properties:
-
PropertyName
—Defines the name of the property that is used to retrieve the filter value. -
Value
—Defines the value used in the comparisons. This is the right operand of the comparison. -
Operator
—Defines theNumericalOperator
value that defines the boolean logic behind the left and right operand comparison.
TimeSpan Filter Descriptor
The TimeSpanFilterDescriptor
is a descriptor which filters by properties of the System.TimeSpan
data type.
The descriptor exposes the following properties:
-
PropertyName
—Defines the name of the property that is used to retrieve the filter value. -
Value
—Defines the value used in the comparisons. This is the right operand of the comparison. -
Operator
—Defines theNumericalOperator
value that defines the boolean logic behind the left and right operand comparison.
Boolean Filter Descriptor
The BooleanFilterDescriptor
is a descriptor which filters by properties of the System.Boolean
data type.
The descriptor exposes the following properties:
-
PropertyName
—Gets or sets the name of the property that is used to retrieve the filter value. -
Value
—Gets or sets the value used in the comparisons. This is the right operand of the comparison.
Nested Property Text Filter Descriptor
The NestedProprtyTextFilterDescriptor
is a descriptor which filters the nested properties.
The descriptor exposes the following properties:
-
PropertyName
—Defines the name of the property that is used to retrieve the filter value. -
Operator
—Defines theTextOperator
value that defines how theValue
member is compared with each value from the items source. -
Value
—Defines the value used in the comparisons. This is the right operand of the comparison. -
IsCaseSensitive
—Defines a value that determines whether the text comparisons will be case-sensitive. The default value istrue
.
Distinct Values Filter Descriptor
The DistinctValuesFilterDescriptor
is a descriptor which filters by distinct values.
The descriptor exposes the following properties:
-
PropertyName
—Defines the name of the property that is used to retrieve the filter value. -
Value
—Defines the value used in the comparisons. This is the right operand of the comparison.
Composite Filter Descriptor
The CompositeFilterDescriptor
represents a special FilterDescriptorBase
that stores an arbitrary number of other descriptors instances. The logical AND
or OR
operator is applied upon all composed filters to determine the result of the PassesFilter
routine.
Delegate Filter Descriptor
The DelegateFilterDescriptor
exposes the Filter
property, which gets or sets the IFilter
implementation used to check whether a data item passes the filter or not.
To use a DelegateFilterDescriptor
, you need to create a class that implements the IFilter
interface which will return the Key
by which you want to filter. Then, you need to add a DelegateFilterDescriptor
to the RadCollectionView.FilterDescriptors
collection and set its Filter
property.