我有TextBox驻留在样式资源字典中的DataGridColumnHeader中,TextBox用作Filter,所以我需要触发TextChanged事件并在View Model中处理它以实现过滤过程。重点是我需要遵循以下规则:< / p>
我做了很多谷歌搜索,但找不到符合所有这些规则的解决方案。 提前致谢
编辑:这是资源字典的完整xaml,其中定义了文本框。 注意:我正在使用MVVM Light EventToCommand尝试绑定TextChanged事件 视图模型中的附加行为的文本框 还要注意,这个xamle仍在开发和修改中,知道这一点 我从DataGrid Filtering Application
获得了大部分内容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>