在DataGrid中实现过滤(文本和组合框)

时间:2012-04-02 14:13:44

标签: wpf wpfdatagrid wpf-4.0

我试图设置一些代码,允许DataBox通过TextBox或ComboBox进行过滤。我已经为TextBox设置了过滤代码,现在对于Combobox类型的过滤器,我不太确定这种方法。

首先,我继承了DataGrid,并将所有过滤代码放在那里。为了将Filter放在datagrid上,我选择了DataGrid的标题。我不想使用继承的DataGrid类中定义的附加属性来控制要显示的过滤器类型。以下是其中一个标识要使用的过滤器类型(文本框或组合框)。

    public class FilteringDataGrid : DataGrid {
        ....
        //Dependency Properties for Combobox or Text search.
        public static DependencyProperty FilterTypeProperty = DependencyProperty.RegisterAttached("FilterType",
                 typeof(FilterTypeEnum), typeof(DataGrid), new PropertyMetadata(FilterTypeEnum.TextBoxOnly));

它设置在DataGridColumn级别。

在DataGrid ColumnHeaderTemplate中,我试图阅读上面附加的属性。但是我不知道如何在ColumnHeaderTemplate中访问Column级别的属性集。我将在Trigger中使用此属性值来呈现TextBox或Combobox作为过滤器。 如何在ColumnHeaderTemplate(或准确的模板触发器)中访问此属性的值

这是DataGrid ColumnHeaderTemplate的相关部分

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:fg="clr-namespace:ThemingControls.CustomControls"> <!--Inherited DataGrid Control namespace -->

                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                                <Grid>
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/> 
                                            </Grid.RowDefinitions>    
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <ContentPresenter Grid.Column="0" Grid.Row="0" 
                                            ....
                                            <Path x:Name="SortArrow"
                                            Grid.Column="1" Grid.Row="0"
                                            ....
     <!-- Combobox or TextBox show either one based on Column FilterType attached Property  -->
                                            <ComboBox Grid.Row="1" Grid.ColumnSpan="2"  IsEditable="False" 
                                                      />
                                            <fg:DelayTextBox Grid.Row="1" Grid.ColumnSpan="2" />
                                            ....

<!-- Triggers to show TextBox/Combobox based on attached property of column -->

                                <ControlTemplate.Triggers>
                                    <Trigger Property="fg:FilteringDataGrid.FilterType" Value="NonEditableComboBox">
                                        <Setter Property="fg:DelayTextBox.Visibility" Value="Collapsed"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>

上面的代码运行但我在所有列中都得到了Combobox类型的过滤器,即使有些列将TextBox设置为附加属性(FilterType)。这意味着上面的触发器不起作用。任何想法如何访问datagrid列中设置的附加属性。即。在ColumnHeaderTemplate中访问它更精确。

1 个答案:

答案 0 :(得分:0)

我会让DataGrid做它的事情 - 显示它给出的数据;而是过滤/排序/分组绑定到数据网格的Itemsource的集合,有大量样本包括MSDN:

http://msdn.microsoft.com/en-us/library/ms752347.aspx#filtering

相关问题