Wpf 在DataGrid中实现过滤(文本和组合框)
我正在尝试编写一些代码,允许通过文本框或组合框过滤数据网格。我已经为TextBox提供了过滤代码,现在对于Combobox类型的过滤器,我不太确定该方法 首先,我继承了DataGrid,我所有的过滤代码都放在那里。为了在datagrid上放置过滤器,我选择了datagrid的标题。我不想使用继承的DataGrid类中定义的附加属性来控制要显示的筛选器类型。下面是其中一个用于标识要使用的过滤器类型(textbox或combobox) 它设置在DataGridColumn级别 在DataGrid ColumnHeaderTemplate中,我试图读取上面附加的属性。但是,我不知道如何访问ColumnHeaderTemplate中列级别的属性集。我将在触发器中使用此属性值来显示TextBox或Combobox作为筛选器如何在ColumnHeaderTemplate(或准确地说是模板触发器)中访问此属性的值 下面是DataGrid ColumnHeaderTemplate的相关部分Wpf 在DataGrid中实现过滤(文本和组合框),wpf,wpfdatagrid,wpf-4.0,Wpf,Wpfdatagrid,Wpf 4.0,我正在尝试编写一些代码,允许通过文本框或组合框过滤数据网格。我已经为TextBox提供了过滤代码,现在对于Combobox类型的过滤器,我不太确定该方法 首先,我继承了DataGrid,我所有的过滤代码都放在那里。为了在datagrid上放置过滤器,我选择了datagrid的标题。我不想使用继承的DataGrid类中定义的附加属性来控制要显示的筛选器类型。下面是其中一个用于标识要使用的过滤器类型(textbox或combobox) 它设置在DataGridColumn级别 在DataGrid C
<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>
我会让DataGrid做它自己的事情——显示给定的数据;而是对绑定到datagrid的Itemsource的集合进行筛选/排序/分组,其中包含大量示例,包括MSDN:
如果是枚举,我认为您引用的值不正确。尝试FilterTypeEnum.NonEditableComboBox实际上,我以前也尝试过,但wpf抱怨“FilterTypeEnum.NonEditableComboBox不是FilterTypeEnum的有效值”。我还尝试使用fg:FilterTypeEnum.NonEditableComboBox,结果是相同的“无效值”,不确定如何在触发器中放置转换器,甚至不确定是否可以完成,但这就是我要尝试的。“你可以看到即将发生的事情,甚至可以抓住并破例。”“唉,现在这里已经很晚了,所以我明天会听从你的建议。”。谢谢你看这个问题。
<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>