By default, when automatic filtering is enabled, a drop down that displays the associated column's distinct values, which can be retrieved through the DistinctValues property of a grid's DataGridContext, is available in all column-manager cells. Through this drop down, which is represented by the AutoFilterControl class, the auto-filter values that are used to automatically filter the data items can be modified by the end user.
The appearance of the auto-filter control and its values can be modified by providing a new style through each column's AutoFilterControlStyle property (see Example 1).
When using an auto-filter control outside of a grid, a ListBox or Selector-based control identified as the PART_DistinctValuesHost template part should be specified in its template, the AutoFilterColumn property must set to the column in the grid whose values are to be filtered, and the AutoFilterContext property must set to the grid's data-grid context (see Example 2). It is not necessary to set the control's ItemsSource property.
The values that are selected in an auto-filter control can be retrieved through the AutoFilterValues property of a grid's data-grid context.
When using external auto-filtering controls with the DataGridCollectionViewSourceBase.DistinctValuesConstraint or the DataGridCollectionViewBase.DistinctValuesConstraint property set to Filtered, and depending on whether you experience performance issues with your data source, your code may need to handle updating the DistinctValues in all of the AutoFilterControl instances. Specifically, performance issues may arise when data is frequently added to, removed from, or replaced in a data source and the DataGridCollectionViewSourceBase.DistinctValuesUpdateMode or the DataGridCollectionViewBase.DistinctValuesUpdateMode property is set to Auto. For this reason, the default value of these properties is Manual. If however you prefer to have the distinct values updated automatically, you must modifiy your code depending on your situation. If performance is not an issue with your data source, you can simply set the mode to Auto when you define your DataGridCollectionViewSource:
If performance is an issue with your data source, leave the DistinctvaluesUpdateMode property set to the default Manual value and have your code update the distinct values at an appropriate time, using the DataGridCollectionViewSourceBase.DistinctValuesRefreshNeeded / DataGridCollectionViewBase.DistinctValuesRefreshNeeded and the DropDownOpened events, as well as the RefreshDistinctValuesForFieldName method (see Example 2 below).
The auto-filter values that are used to automatically filter a column's values can be cleared by calling the Clear method of any AutoFilterValues collection, using the "Clear" button in the default auto-filter drop down, or by handling the auto-filter control's ClearAutoFilterValues command.
The ClearAutoFilterValues command can be used to clear auto-filter values by assigning it to the Command property of the desired control (usually a Button) and setting the CommandTarget property to the target the auto-filter control whose values to clear (see Example 2).
The auto-filter values that are used to automatically filter a column's values can also all be selected, by using the "Select All" button in the default auto-filter drop down or by handling the auto-filter control's ClearAutoFilterValues command.
The SelectAllAutoFilterValues command can be used to select all of the values by assigning it to the Command property of the desired control (usually a Button) and setting the CommandTarget property to the target the auto-filter control whose values to select.All examples in this topic assume that the grid is bound to the Orders table of the Northwind database, unless stated otherwise.
Example 1: Providing a new auto-filter-control style
The following example demonstrates how to provide the ShipCountry column with a new style for its associated AutoFilterControl that will only allow single selection.
| XAML |
Copy Code |
|---|---|
<Grid> <Grid.Resources> <xcdg:DataGridCollectionViewSource x:Key="cvs_orders" Source="{Binding Source={x:Static Application.Current}, Path=Orders}" AutoFilterMode="And" DistinctValuesConstraint="Filtered" AutoCreateItemProperties="False"> <xcdg:DataGridCollectionViewSource.ItemProperties> <xcdg:DataGridItemProperty Name="ShipCountry" Title="Country"/> <xcdg:DataGridItemProperty Name="ShipCity" Title="City"/> <xcdg:DataGridItemProperty Name="ShipAddress" Title="Address"/> <xcdg:DataGridItemProperty Name="ShipPostalCode" Title="Postal Code"/> <xcdg:DataGridItemProperty Name="ShipName" Title="Name" CalculateDistinctValues="False"/> <xcdg:DataGridItemProperty Name="OrderDate" Title="Order Date" CalculateDistinctValues="False"/> <xcdg:DataGridItemProperty Name="Freight" CalculateDistinctValues="False"/> </xcdg:DataGridCollectionViewSource.ItemProperties> </xcdg:DataGridCollectionViewSource> <Style x:Key="autoFilterControlStyle" TargetType="{x:Type xcdg:AutoFilterControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <ListBox x:Name="PART_DistinctValuesHost" SelectionMode="Single"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </Grid.Resources> <xcdg:DataGridControl x:Name="OrdersGrid" ItemsSource="{Binding Source={StaticResource cvs_orders}}"> <xcdg:DataGridControl.Columns> <xcdg:Column FieldName="ShipCountry" AutoFilterControlStyle="{StaticResource autoFilterControlStyle}"/> </xcdg:DataGridControl.Columns> </xcdg:DataGridControl> </Grid> | |
Example 2: Creating external auto-filtering controls
The following example demonstrates how to use a ComboBox as an auto-filter control to automatically filter the content of the ShipCountry column. ComboBox controls do not support multiple selections; therefore, the values of the target column will only be filtered by 1 value.
Since, by default, the auto-filter control in the column-manager-cell drop downs support multiple selections, it is recommended to deactivate the drop down by setting the AllowAutoFilter property of the ColumnManagerRow to false to hide the column-manager cells' auto-filter controls and prevent unexpected synchronization behavior between the controls that have the same auto-filter target column or different selection modes.
| XAML |
Copy Code |
|---|---|
<Grid> <Grid.Resources> <xcdg:DataGridCollectionViewSource x:Key="cvsOrders" Source="{Binding Source={x:Static Application.Current},Path=Orders}" AutoFilterMode="And" AutoCreateItemProperties="False" DefaultCalculateDistinctValues="False" DistinctValuesConstraint="Filtered" DistinctValuesRefreshNeeded="cvsOrders_DistinctValuesRefreshNeeded"> <xcdg:DataGridCollectionViewSource.ItemProperties> <xcdg:DataGridItemProperty Name="ShipCountry" Title="Country" CalculateDistinctValues="True" /> <xcdg:DataGridItemProperty Name="ShipCity" Title="City" CalculateDistinctValues="True" /> <xcdg:DataGridItemProperty Name="ShipAddress" Title="Address" /> <xcdg:DataGridItemProperty Name="ShipPostalCode" Title="Postal Code" /> <xcdg:DataGridItemProperty Name="ShipName" Title="Name" /> <xcdg:DataGridItemProperty Name="OrderDate" Title="Order Date" /> <xcdg:DataGridItemProperty Name="Freight" /> </xcdg:DataGridCollectionViewSource.ItemProperties> </xcdg:DataGridCollectionViewSource> </Grid.Resources> <DockPanel> <StackPanel Margin="20,0,0,0" Orientation="Horizontal" DockPanel.Dock="Top" HorizontalAlignment="Left"> <xcdg:AutoFilterControl x:Name="ShipCountryAutoFilterControl" AutoFilterColumn="{Binding ElementName=dataGrid, Path=Columns[ShipCountry]}" AutoFilterContext="{Binding ElementName=dataGrid, Path=DataGridContext}"> <xcdg:AutoFilterControl.Template> <ControlTemplate TargetType="{x:Type xcdg:AutoFilterControl}"> <StackPanel> <Button Content="Clear Country filter" Command="xcdg:AutoFilterControl.ClearAutoFilterValues" CommandTarget="{Binding ElementName=ShipCountryAutoFilterControl}" /> <ComboBox x:Name="PART_DistinctValuesHost" DropDownOpened="ShipCountryAutoFilterControl_DropDownOpened" /> </StackPanel> </ControlTemplate> </xcdg:AutoFilterControl.Template> </xcdg:AutoFilterControl> <xcdg:AutoFilterControl x:Name="ShipCityAutoFilterControl" AutoFilterColumn="{Binding ElementName=dataGrid, Path=Columns[ShipCity]}" AutoFilterContext="{Binding ElementName=dataGrid, Path=DataGridContext}"> <xcdg:AutoFilterControl.Template> <ControlTemplate TargetType="{x:Type xcdg:AutoFilterControl}"> <StackPanel> <Button Content="Clear City filter" Command="xcdg:AutoFilterControl.ClearAutoFilterValues" CommandTarget="{Binding ElementName=ShipCityAutoFilterControl}" /> <ComboBox x:Name="PART_DistinctValuesHost" DropDownOpened="ShipCityAutoFilterControl_DropDownOpened" /> </StackPanel> </ControlTemplate> </xcdg:AutoFilterControl.Template> </xcdg:AutoFilterControl> </StackPanel> <xcdg:DataGridControl x:Name="dataGrid" ItemsSource="{Binding Source={StaticResource cvsOrders}}"> <xcdg:DataGridControl.View> <xcdg:TableflowView UseDefaultHeadersFooters="False"> <xcdg:TableflowView.FixedHeaders> <DataTemplate> <xcdg:GroupByControl /> </DataTemplate> <DataTemplate> <xcdg:ColumnManagerRow AllowAutoFilter="False" /> </DataTemplate> </xcdg:TableflowView.FixedHeaders> </xcdg:TableflowView> </xcdg:DataGridControl.View> </xcdg:DataGridControl> </DockPanel> </Grid> | |
The following code provides the code-behind implementation of the DistinctValuesRefreshNeeded and DropDownOpened events.