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

时间:2013-04-20 13:59:41

标签: wpf mvvm

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

  1. 没有代码背后
  2. 视图模型中的事件处理
  3. USe样式能够始终如一地为所有DataGrids应用
  4. 将事件参数传递给视图模型以在过滤过程中使用
  5. 我做了很多谷歌搜索,但找不到符合所有这些规则的解决方案。 提前致谢

    编辑:这是资源字典的完整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>
    

0 个答案:

没有答案