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.