Click here to Skip to main content
12,299,734 members (56,912 online)
Click here to Skip to main content

Stats

118.7K views
5.7K downloads
56 bookmarked
Posted

Auto-filter for Microsoft WPF DataGrid

, 29 Jan 2009 Eclipse
Allows auto filtering functionality for DataGrid columns.
WPFGridFilter
PowerCollections
PowerCollections.csproj.user
Properties
Stepi.Collections
bin
Filters
Properties
Stepi.UIFilters
bin
Initializers
Properties
Themes
Test.GridSorting
bin
Debug
Test.GridFilter.vshost.exe
Properties
<Window x:Class="Test.GridSorting.MainWnd"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        
        xmlns:stepi="clr-namespace:Stepi.UIFilters;assembly=Stepi.UIFilters"
        xmlns:local="clr-namespace:Test.GridSorting"
        xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
        xmlns:primitives="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit"
        
        Title="Window1" Height="400" Width="700">
    <Window.Resources>

        <Style x:Key="ColumnHeaderGripperStyle" TargetType="{x:Type Thumb}">
            <Setter Property="Width" Value="8"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Cursor" Value="SizeWE"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Thumb}">
                        <Border Padding="{TemplateBinding Padding}"
                    Background="{TemplateBinding Background}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="primitives:DataGridColumnHeader" x:Key="columnHeaderStyle" >
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type primitives:DataGridColumnHeader}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <dg:DataGridHeaderBorder SortDirection="{TemplateBinding SortDirection}"
                                                     IsHovered="{TemplateBinding IsMouseOver}"
                                                     IsPressed="{TemplateBinding IsPressed}"
                                                     IsClickable="{TemplateBinding CanUserSort}"
                                                     Background="{TemplateBinding Background}"
                                                     BorderBrush="{TemplateBinding BorderBrush}"
                                                     BorderThickness="{TemplateBinding BorderThickness}"
                                                     Padding ="{TemplateBinding Padding}"
                                                     SeparatorVisibility="{TemplateBinding SeparatorVisibility}"
                                                     SeparatorBrush="{TemplateBinding SeparatorBrush}"
                                                      Grid.ColumnSpan="2"/>
                                <ContentPresenter Margin="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>                          
                            <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" 
                               Style="{StaticResource ColumnHeaderGripperStyle}"/>
                            <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right"  Grid.Column="1"
                               Style="{StaticResource ColumnHeaderGripperStyle}"/>
                            
                            
                            <!-- InitalizersManager="{StaticResource filtersViewInitializersManager}" if you want a different initializers list-->
                            <stepi:DataGridColumnFilter Grid.Row="0" Grid.Column="1"
                                                               Background="Transparent"
                                                               Width="Auto" Height="Auto"
                                                               Margin="4,1,4,1"
                                                               HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="DisplayIndex" Value="0">
                                <Setter Property="Visibility" Value="Collapsed" TargetName="PART_LeftHeaderGripper"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.8*"/>
            <ColumnDefinition Width="0.2*"/>
        </Grid.ColumnDefinitions>

        <CheckBox IsChecked="{Binding IsFakeUpdateEnabled, Mode=TwoWay}" Content="Fake data update"/>
        <dg:DataGrid  x:Name="dg"
                        Grid.ColumnSpan="2" Grid.Row="1"
                        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                        ItemsSource="{Binding Data}"
                         ColumnHeaderStyle="{StaticResource columnHeaderStyle}"
                        Margin="2" BorderThickness="1" BorderBrush="Black" AutoGenerateColumns="False">
            <!---->
            <dg:DataGrid.Columns>
                <dg:DataGridTextColumn Header="First Name" Binding="{Binding FirstName, Mode=TwoWay}" />
                <dg:DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
                <dg:DataGridTextColumn Header="Age" Width="70" Binding="{Binding Age}" />

                <dg:DataGridTemplateColumn Header="Birthday" MinWidth="100">
                    <dg:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <dg:DatePicker SelectedDate="{Binding Birthday}" SelectedDateFormat="Short" />
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellEditingTemplate>
                    <dg:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Birthday, StringFormat=d}" />
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellTemplate>
                </dg:DataGridTemplateColumn>
                <dg:DataGridTextColumn Header="Value" Width="150" Binding="{Binding Value}" />
            </dg:DataGrid.Columns>
       </dg:DataGrid>
    </Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Eclipse Public License 1.0

Share

About the Author

Stefan Bocutiu
Software Developer (Senior) Lab49
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 29 Jan 2009
Article Copyright 2009 by Stefan Bocutiu
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid