In This Topic
    In This Topic

    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.

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //Reset all flags
            m_distinctValuesNeedRefreshFlags.Add("ShipCountry", false);
            m_distinctValuesNeedRefreshFlags.Add("ShipCity", false);
        }
        private void cvsOrders_DistinctValuesRefreshNeeded(object sender, EventArgs e)
        {
            //Use this event to set flags that will tell the AutoFilterControl it needs to update when its DropDown is opening.
            IList<string> keys = m_distinctValuesNeedRefreshFlags.Keys.ToList();
            for (int i = 0; i < keys.Count; i++)
            {
                m_distinctValuesNeedRefreshFlags[keys[i]] = true;
            }
        }
        private void ShipCountryAutoFilterControl_DropDownOpened(object sender, EventArgs e)
        {
            //If DistinctValues need to be updated
            if (m_distinctValuesNeedRefreshFlags["ShipCountry"])
            {
                //Get the collection view
                DataGridCollectionView collectionView = dataGrid.ItemsSource as DataGridCollectionView;
                if (collectionView != null)
                {
                    //And refresh the DistinctValues only for the required field
                    collectionView.RefreshDistinctValuesForFieldName("ShipCountry");
                }
                //Reset the flag.
                m_distinctValuesNeedRefreshFlags["ShipCountry"] = false;
            }
        }
        private void ShipCityAutoFilterControl_DropDownOpened(object sender, EventArgs e)
        {
            //If DistinctValues need to be updated
            if (m_distinctValuesNeedRefreshFlags["ShipCity"])
            {
                //Get the collection view
                DataGridCollectionView collectionView = dataGrid.ItemsSource as DataGridCollectionView;
                if (collectionView != null)
                {
                    //And refresh the DistinctValues only for the required field
                    collectionView.RefreshDistinctValuesForFieldName("ShipCity");
                }
                //Reset the flag.
                m_distinctValuesNeedRefreshFlags["ShipCity"] = false;
            }
        }
        private Dictionary<string, bool> m_distinctValuesNeedRefreshFlags = new Dictionary<string, bool>();
    }
    Public Partial Class MainWindow
     Inherits Window
     Public Sub New()
      InitializeComponent()
      'Reset all flags
      m_distinctValuesNeedRefreshFlags.Add("ShipCountry", False)
      m_distinctValuesNeedRefreshFlags.Add("ShipCity", False)
     End Sub
     Private Sub cvsOrders_DistinctValuesRefreshNeeded(sender As Object, e As EventArgs)
      'Use this event to set flags that will tell the AutoFilterControl it needs to update when its DropDown is opening.
      Dim keys As IList(Of String) = m_distinctValuesNeedRefreshFlags.Keys.ToList()
      For i As Integer = 0 To keys.Count - 1
       m_distinctValuesNeedRefreshFlags(keys(i)) = True
      Next
     End Sub
     Private Sub ShipCountryAutoFilterControl_DropDownOpened(sender As Object, e As EventArgs)
      'If DistinctValues need to be updated
      If m_distinctValuesNeedRefreshFlags("ShipCountry") Then
       'Get the collection view
       Dim collectionView As DataGridCollectionView = TryCast(dataGrid.ItemsSource, DataGridCollectionView)
       If collectionView IsNot Nothing Then
        'And refresh the DistinctValues only for the required field
        collectionView.RefreshDistinctValuesForFieldName("ShipCountry")
       End If
       'Reset the flag.
       m_distinctValuesNeedRefreshFlags("ShipCountry") = False
      End If
     End Sub
     Private Sub ShipCityAutoFilterControl_DropDownOpened(sender As Object, e As EventArgs)
      'If DistinctValues need to be updated
      If m_distinctValuesNeedRefreshFlags("ShipCity") Then
       'Get the collection view
       Dim collectionView As DataGridCollectionView = TryCast(dataGrid.ItemsSource, DataGridCollectionView)
       If collectionView IsNot Nothing Then
        'And refresh the DistinctValues only for the required field
        collectionView.RefreshDistinctValuesForFieldName("ShipCity")
       End If
       'Reset the flag.
       m_distinctValuesNeedRefreshFlags("ShipCity") = False
      End If
     End Sub
     Private m_distinctValuesNeedRefreshFlags As New Dictionary(Of String, Boolean)()
    End Class