Skip Navigation LinksHome / Community & Support / Code Library / WPF > GridView > Custom Filtering using a Combo Box

Not answered Custom Filtering using a Combo Box

Feed from this thread
  • Atanas avatar

    Posted on Oct 8, 2008 (permalink)

    Requirements

    RadControls version

    Q2 2008 SP 1
    .NET version   3.5
    Visual Studio version 2008 SP 1
    programming language

    C#

     
    PROJECT DESCRIPTION
    In the sample project a RadGridView uses combo boxes to perform filtering. First, we create a control template for the DistinctFilterControl, the control that realizes the filtering process.
     
            <ControlTemplate x:Key="FilterControlTemplate" TargetType="{x:Type telerik:DistinctFilterControl}"
                <StackPanel> 
                    <StackPanel Orientation="Horizontal"
                        <ComboBox IsEditable="True" IsReadOnly="False" Name="comboBoxDistinctValues" 
                                    MaxHeight="500" HorizontalAlignment="Stretch" Width="70" 
                                    ItemsSource="{TemplateBinding DistinctValues}" 
                                    Loaded="ComboBox_Loaded"  
                                    SelectionChanged="ComboBox_SelectionChanged" /> 
                        <telerik:ClearFilterButton Margin="0,0,0,5" Content="Clear Filter" 
                                                Target="{Binding ElementName=comboBoxDistinctValues, Mode=OneTime}"  
                                                FilterDescription ="{TemplateBinding FilterDescription}" /> 
                    </StackPanel> 
                </StackPanel> 
            </ControlTemplate> 
            <Style TargetType="{x:Type telerik:DistinctFilterControl}"
                <Setter Property="Template" Value="{StaticResource FilterControlTemplate}" /> 
            </Style> 

    You can see the combo box (named "comboBoxDistinctValues") that contains the values you can filter by. Our button, that clears your filter, is retained in the template.

    You will notice that the events Loaded and SelectionChanged of the combo box are handled in the code-behind. The handler of Loaded:

     
    ComboBox comboBox = (ComboBox) sender; 
    DistinctFilterControl filterControl = this.GetVisualParent<DistinctFilterControl>(comboBox); 
    this.PopulateDistinctValues(string.Empty, filterControl); 

    calls the method "PopulateDistinctValues":
     
    GridViewHeaderCell headerCell = this.GetVisualParent<GridViewHeaderCell>(filterControl); 
     
    string propertyName = ((IColumnInfo) headerCell).PropertyName; 
     
    GridViewDataControl gridControl = this.GetVisualParent<GridViewDataControl>(filterControl); 
    filterControl.DistinctValues = gridControl.ItemsControl.RecordManager.GetDistinctValues(propertyName); 

    which fills the combo box with the values you can use for filtering. The event handler for SelectionChanged:

     
    ComboBox comboBox = (ComboBox) sender; 
    DistinctFilterControl filterControl = this.GetVisualParent<DistinctFilterControl>(comboBox); 
    FieldFilterDescription filterDescription = filterControl.FilterDescription; 
     
    IList valueCollection; 
    if (e.AddedItems.Count > 0) 
       valueCollection = e.AddedItems; 
       foreach (object value in valueCollection) 
       { 
           filterDescription.Values.Add(value); 
       } 
     
    if (e.RemovedItems.Count > 0) 
        valueCollection = e.RemovedItems; 
        foreach (object value in valueCollection) 
        { 
            if ((comboBox.Items.IndexOf(value) != -1)) 
                filterDescription.Values.Remove(value); 
        } 

    changes the filtering whenever you select a new value in the combo box.
    Attached files

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Code Library / WPF > GridView > Custom Filtering using a Combo Box