The data contained in a listbox can be filtered (searched), programmatically or through end-user interaction, to display a subset of data. Programmatically, data can be filtered through the Filter event, which allows more complex filtering such as comparisons between two fields, by providing a filter expression (see FilterExpression class) via the FilterExpression property, or through a combination of both.
End-users can filter the data in a listbox by typing in the SearchTextBox. A search text box (and the FilterControl that contains it) is displayed in a tool pane by default, but is disabled. To hide the tool pane and therefore the search text box, set ToolPaneVisibility to Visibility.Collapsed. To activate the search text box, the FilteredFieldNames attached property must be set to a FieldNameList instance representing the fields by which the data can be filtered. The following XAML will allow the listbox to filter data by the Country field.
The SearchValueDelimiters property on the SearchTextBox class contains the characters that are accepted as delimiters between search values.
The FilterControl class acts as a delay-providing intermediary between the SearchTextBox and the ListBox. As the end-user types in the SearchTextBox, the UIFilterExpression property is updated continuously (being bound to SearchTextBox.FilterExpression through two-way binding in the product's default templates) to contain the filter that should be applied to the data. However, the FilterControl waits for the amount of time represented by ApplyFilterInterval to pass, without any other changes being made in the SearchTextBox, before DataSourceFilterExpression (bound to ListBox.FilterExpression in the product's default templates) is updated using UIFilterExpression. The advantage of this approach is to avoid the filter being applied after each keystroke. By default, the delay value is a TimeSpan representing 700 milliseconds. The FilterControl's ClearFilterButtonVisibility property allows the clear filter button to be hidden. The ClearFilterCommand is an ICommand representing the command associated with the clear filter button.
The filter expression, which is accessed through a listbox's FilterExpression property, is imbedded into the data query that is used to request data and filters the items before they are returned. The filter expression that is provided can be a simple, single-value expression (see FilterExpression class), or a more complex expression using one or more logical filter expressions (see AndFilterExpression, NotFilterExpression, and OrFilterExpression classes). Each filter expression defines a value, the name of the member by whose values the items are to be filtered, and the filter operator to use (see FilterOperator enumeration). If a logical filter expression is used, at least two operand filter expressions must be provided.
The Filter event is raised for each item in the data source that has not already been excluded by a filter expression to determine if it passes a filter. Unlike filter expressions, the Filter event can be used to provide more complex filtering such as comparisons between two fields.