gpt4 book ai didi

wpf - 如何在样式资源字典中从 DataGridColumnHeader 中的文本框中触发和处理 TextChanged 事件

转载 作者:行者123 更新时间:2023-12-03 10:19:36 26 4
gpt4 key购买 nike

我有 TextBox 驻留在样式资源字典中的 DataGridColumnHeader 中,TextBox 用作过滤器,所以我需要触发 TextChanged 事件并在 View Model 中处理它以实现过滤过程。重点是我需要遵守以下规则:

  • 后面没有代码
  • View Model 中的事件处理
  • 使用样式能够一致地将其应用于所有 DataGrid
  • 将事件参数传递给 View 模型以在过滤过程中使用

  • 我做了很多谷歌搜索,但找不到符合所有这些规则的解决方案。
    提前致谢

    编辑:这是定义文本框的资源字典的完整 xaml。
    注意:我正在使用 MVVM Light EventToCommand 尝试绑定(bind) TextChanged 事件
    View 模型中附加行为的文本框
    另请注意,此 xamle 仍在开发和修改中,知道
    我从 DataGrid 过滤应用程序中得到了大部分
    enter code here
    <ResourceDictionary x:Class="PreCommissioning.Infrastructure.Filter2.FilteringDataGrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
    xmlns:local="clr-namespace:PreCommissioning.Infrastructure.Filter2;assembly=PreCommissioning.Infrastructure"
    xmlns:localx="clr-namespace:PreCommissioning.Module.Subsystem.ViewModel;assembly=PreCommissioning.Module.Subsystem"
    xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
    >

    <local:HeaderFilterConverter x:Key="headerConverter"/>

    <Style TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
    <ControlTemplate.Resources>
    <Storyboard x:Key="ShowFilterControl">
    <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="filterTextBox" Storyboard.TargetProperty="(UIElement.Visibility)">
    <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>
    <DiscreteObjectKeyFrame KeyTime="00:00:00.5000000" Value="{x:Static Visibility.Visible}"/>
    </ObjectAnimationUsingKeyFrames>
    <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="filterTextBox" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
    <SplineColorKeyFrame KeyTime="00:00:00" Value="Transparent"/>
    <SplineColorKeyFrame KeyTime="00:00:00.5000000" Value="White"/>
    </ColorAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="HideFilterControl">
    <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="filterTextBox" Storyboard.TargetProperty="(UIElement.Visibility)">
    <DiscreteObjectKeyFrame KeyTime="00:00:00.4000000" Value="{x:Static Visibility.Collapsed}"/>
    </ObjectAnimationUsingKeyFrames>
    <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="filterTextBox" Storyboard.TargetProperty="(UIElement.OpacityMask).(SolidColorBrush.Color)">
    <SplineColorKeyFrame KeyTime="00:00:00" Value="Black"/>
    <SplineColorKeyFrame KeyTime="00:00:00.4000000" Value="#00000000"/>
    </ColorAnimationUsingKeyFrames>
    </Storyboard>
    </ControlTemplate.Resources>
    <my:DataGridHeaderBorder x:Name="dataGridHeaderBorder" Margin="0" VerticalAlignment="Top" Height="31" IsClickable="{TemplateBinding CanUserSort}" IsHovered="{TemplateBinding IsMouseOver}" IsPressed="{TemplateBinding IsPressed}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}" SortDirection="{TemplateBinding SortDirection}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.ColumnSpan="1">
    <Grid x:Name="grid" Width="Auto" Height="Auto" RenderTransformOrigin="0.5,0.5">
    <Grid.RenderTransform>
    <TransformGroup>
    <ScaleTransform/>
    <SkewTransform/>
    <RotateTransform/>
    <TranslateTransform/>
    </TransformGroup>
    </Grid.RenderTransform>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <ContentPresenter x:Name="contentPresenter"
    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
    ContentStringFormat="{TemplateBinding ContentStringFormat}"
    ContentTemplate="{TemplateBinding ContentTemplate}">
    <ContentPresenter.Content>
    <MultiBinding Converter="{StaticResource headerConverter}">
    <MultiBinding.Bindings>
    <Binding ElementName="filterTextBox" Path="Text" />
    <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Content" />
    </MultiBinding.Bindings>
    </MultiBinding>
    </ContentPresenter.Content>
    </ContentPresenter>

    <!--Filter Text Box-->
    <TextBox x:Name="filterTextBox" HorizontalAlignment="Right"
    MinWidth="25" Height="Auto" OpacityMask="Black" Visibility="Collapsed"
    Text=""
    TextWrapping="Wrap" Grid.Column="0" Grid.ColumnSpan="1">
    <i:Interaction.Triggers>
    <i:EventTrigger EventName="TextChanged">
    <cmd:EventToCommand Command="{Binding Path=DataContext.TextChangedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
    PassEventArgsToCommand="True"
    CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBox}}, Path=Name}"/>
    </i:EventTrigger>
    </i:Interaction.Triggers>
    </TextBox>

    </Grid>
    </my:DataGridHeaderBorder>
    <ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
    <Trigger.EnterActions>
    <BeginStoryboard x:Name="ShowFilterControl_BeginStoryboard" Storyboard="{StaticResource ShowFilterControl}"/>
    <StopStoryboard BeginStoryboardName="HideFilterControl_BeginShowFilterControl"/>
    </Trigger.EnterActions>
    <Trigger.ExitActions>
    <BeginStoryboard x:Name="HideFilterControl_BeginShowFilterControl" Storyboard="{StaticResource HideFilterControl}"/>
    <StopStoryboard BeginStoryboardName="ShowFilterControl_BeginStoryboard"/>
    </Trigger.ExitActions>
    </Trigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>


    <Style x:Key="DataGridStyle" TargetType="DataGrid">
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="AlternatingRowBackground" Value="#00FFFFFF"/>
    <Setter Property="VerticalGridLinesBrush" Value="#FFC9CACA"/>
    <Setter Property="HorizontalGridLinesBrush" Value="#FFC9CACA"/>
    <Setter Property="AutoGenerateColumns" Value="False"/>
    <!--

    -->
    <Style.Triggers>
    <Trigger Property="IsGrouping" Value="true">
    <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
    </Trigger>
    </Style.Triggers>
    </Style>


    </ResourceDictionary>

    最佳答案

    我在我的应用程序中实现了一个非常相似的过滤器文本。它与命令一起使用,将参数作为当前文本。我希望这就是你要找的东西(看到这个问题这些年来仍然没有答案有点晚了)

    <StackPanel Grid.Row="0" Orientation="Horizontal">
    <Label Content="Filter names by " ></Label>
    <TextBox Name="txtFilter" Width="290" Height="25" VerticalAlignment="Center" Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged}"
    BorderBrush="DarkGray" BorderThickness="0,0,0,1">
    <i:Interaction.Triggers>
    <i:EventTrigger EventName="TextChanged">
    <i:InvokeCommandAction Command="{Binding FilterItemsCommand}"></i:InvokeCommandAction>
    </i:EventTrigger>
    </i:Interaction.Triggers>
    </TextBox>
    </StackPanel>

    在各自的 ViewModel 中,我有以下代码行来使用过滤器操作。

    ViewModel 构造函数:
    FilterItemsCommand = new RelayCommand(_ => FilterItems(), _ => CanFilterItems);
    FilterText = string.Empty;

    和过滤器命令逻辑:
        [NotifyPropertyChanged]
    public string FilterText { get; set; }
    public ICommand FilterItemsCommand
    {
    get;
    set;
    }

    public void FilterItems()
    {
    // to load the items from source so every filter change will be from the source.
    if (string.IsNullOrWhiteSpace(FilterText))
    PopulateItems(); //imagine i am loading a set of items from source to display in the UI
    else PopulateItems(filter: FilterText);
    }

    public bool CanFilterItems
    {
    get
    {
    return (Items != null);
    }
    }

    如果您对 PopulateItems 方法感兴趣,
    private void PopulateLaunchItems(string filter = "")
    {
    // Add items
    Items = new ObservableCollection<SomeItemType>();

    // use your filter to manipulate the data from source
    ...
    ...
    }

    这是一段工作代码。我希望这对将来的某人有所帮助,即使最初被问到的人可能已经继续前进。 ;-)

    关于wpf - 如何在样式资源字典中从 DataGridColumnHeader 中的文本框中触发和处理 TextChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16121156/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com