【问题标题】:WPF create filter by text box header columnWPF按文本框标题列创建过滤器
【发布时间】:2021-10-31 18:03:44
【问题描述】:

我正在尝试在我的 dataGrid 中为每个列标题创建一个基于文本的过滤器。我已经设法通过数据模板添加了文本框。

因此,一旦用户在列标题文本框中输入文本,网格将仅按该列过滤。

这就是它的样子

但我不知道如何使用 TextChanged 事件,因为我得到了一个

ResourceDictionary' 根元素需要 x:Class 属性来支持 XAML 文件中的事件处理程序

错误。

这是我目前的补充

<Grid.Resources>
                <Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource DataGridColumnHeaderStyle}" >
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Background="{DynamicResource ListHeaderBackgroundBrush}">
                                    <TextBlock Text="{Binding}" TextWrapping="Wrap"/>
                                    <TextBox TextChanged="OnTextChanged"></TextBox> <!--per column filter-->
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>

                </Style>
            </Grid.Resources>

这是整个 sn-p,包括对如何定义列的引用。

 <Style x:Key="ExtraDataTabItemStyle" TargetType="{x:Type TabItem}" BasedOn="{StaticResource TabItemStyle}" >
        <Setter Property="Visibility"  Value="{Binding Path=HasItems, Converter={extensions:VisibilityConverter HideOnly=False}}" />
    </Style>
    <DataTemplate x:Key="ExtraDataTabItemTemplate"  DataType="{x:Type my:StudyExtraDataViewModel}">
        <Grid Width="Auto" Height="Auto">
            <TextBlock Text="{Binding Path=Header}"></TextBlock>
        </Grid>
    </DataTemplate>
    <!-- Custom styling for context menu items -->
    <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource ContextMenuItemStyle}">
    </Style>
    <DataTemplate DataType="{x:Type my:StudiesViewModel}">
        <Grid >
            <Grid.Resources>
                <Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource DataGridColumnHeaderStyle}" >
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Background="{DynamicResource ListHeaderBackgroundBrush}">
                                    <TextBlock Text="{Binding}" TextWrapping="Wrap"/>
                                    <TextBox TextChanged="OnTextChanged"></TextBox> <!--per column filter-->
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                    
                </Style>
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition Height="{Binding StudiesGridHeight, Mode=TwoWay}" MinHeight="150"/>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="{Binding SeriesGridHeight, Mode=TwoWay}" MinHeight="200" />
            </Grid.RowDefinitions>
            <controls:DataGridExtended  Grid.Row="0" x:Name="studiesGrid" DataContext="{Binding}" ItemsSource="{Binding Path=Studies}"
                                        EnableRowVirtualization ="False"
                                        Margin="10,10,10,10"
                                        AutoGenerateColumns ="False"
                                        AreRowDetailsFrozen="False"
                                        ColumnsPreferences="{Binding Path=Columns}"
                                        ColumnsForcedToPresent="{Binding Path=ColumnsForcedToPresent}"
                                        Style="{DynamicResource DataGridStyleExtended}"
                                        UserSelectionChanged="OnUserSelectionChanged"
                                        SourceSelectionChanged="OnSourceSelectionChanged"                                                                                 
                                        ColumnHeaderContextMenu="{Binding Path=ColumnHeaderContextMenu}"
                                        >
                <DataGrid.RowStyle>
                    <Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource DataGridRowStyle}">
                        <Setter Property="IsSelected" Value="{Binding Selected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                        <Setter Property="behaviors:ClickBehavior.DoubleClick" 
                                Value="{Binding ElementName=studiesGrid, Path=DataContext.StudyDoubleClickCommand}"/>
                    </Style>
                </DataGrid.RowStyle>
           
                <DataGrid.Columns>
                    
                    <DataGridTextColumn controls:DataGridExtended.ColumnId="StudyId"
                                        Header="{wpfCommon:LocalizeExtension Key=StudyId, Type='Systems.CommonComponents.UI.StudySelectorWindow, CommonComponents'}"     
                                        Binding="{Binding StudyId}"
                                        ElementStyle="{DynamicResource TextBlockStyle}"
                                        CanUserSort="True">
                        

                    </DataGridTextColumn>

【问题讨论】:

  • 我认为错误信息很清楚。要附加事件处理程序,根元素必须设置 x:Class 属性,以便拥有一个部分类代码隐藏文件,该文件实际上允许定义事件处理程序,如 OnTextChanged 处理程序。
  • @BionicCode 将 StudiesViewModel 设置为 x:class 我从 DataTemplrate DataType 行“”也收到错误,viewModel 类从不同的类继承
  • 不,根元素必须是它自己的类。然后在 .cs 文件中创建一个实用类以获得 C# 代码。与 MainWindow 相同的原理是 Window 元素的 x:Class,而 MainWindow.xaml.cs 包含部分类定义 MainWindow。您可以将您的样式添加到元素的 ResourceDictionary 中,该元素的 ResourceDictionary 已经有一个代码隐藏文件,例如 MainWindow.Resources 或 Application.Resources(在 App.xaml 中)
  • *.cs 文件应命名为 MyResourceDictionary.xaml.cs,其中 MyResourceDictionary.xaml 是 xaml 文件名。
  • x:Class 属性指向 ResourceDictionary 的最顶层元素,即 RootElement - 而不是像 DataTemplate 这样的包含资源 第一行通常看起来像 &lt;ResourceDictionary x:Class="...."

标签: c# wpf user-interface


【解决方案1】:

如果您的Style 是在ResourceDictionary 中定义的,您需要在其中定义事件处理程序的位置添加一个代码隐藏类。

  1. x:Class 属性添加到 XAML 标记的根元素:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 x:Class="YourProjectNamespace.MyResourceDictionary"
                 ...
    
  2. 添加一个名为 MyResourceDictionary 的类。xaml.cs(其中“MyResourceDictionary”是定义 Style 的资源字典的当前名称)并从 ResourceDictionary 派生它。 p>

  3. 将事件处理程序添加到新类中:

     namespace YourProjectNamespace
     {
         partial class MyResourceDictionary : ResourceDictionary
         {
             public MyResourceDictionary()
             {
                 InitializeComponent();
             }
    
             private void OnTextChanged(object sender, TextChangedEventArgs e)
             {
                  //...
             }
         }
     }
    

【讨论】:

  • 那么 SourceSelectionChanged="OnSourceSelectionChanged" 是如何工作的呢?你不必每个文件都有代码,对吧?
猜你喜欢
  • 1970-01-01
  • 2014-10-22
  • 2019-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多