WPF-MVVM:来自DataGrid Style的Handeling事件

时间:2013-04-18 13:52:27

标签: wpf mvvm

我开发基于WPF + MVVM的Prism + MEF应用程序,其中有许多DataGrids,因此我构建DataGridStyle以应用于所有模块中的所有DataGrids。样式在列标题中添加过滤器文本框,我使用MVVM Light EventToCommand在文本框文本更改时触发TextChanged事件:(此代码存在于DataGridStyle资源字典中)

    <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 DataContext.TextChangedCommand}"
                          PassEventArgsToCommand="True"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
   </TextBox>

然后我使用附加属性处理ViewModel中的TextChangedCommand(属于包含数据网格的View):

#region TextChangedCommand----------------------------------------------------------------------------------------------

        static ICommand command; //1

        public static ICommand GetTextChangedCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(TextChangedCommandProperty);
        }

        public static void SetTextChangedCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(TextChangedCommandProperty, value);
        }

        // Using a DependencyProperty as the backing store for TextChangedCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextChangedCommandProperty =
            DependencyProperty.RegisterAttached("TextChangedCommand",
                                                 typeof(ICommand), 
                                                 typeof(SubsystemAllViewModel),
                                                 new UIPropertyMetadata(null, CommandChanged));


        static void CommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var fe = obj as FrameworkElement;
            command = e.NewValue as ICommand;
            fe.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(ExecuteCommand));
        }

        static void ExecuteCommand(object sender, TextChangedEventArgs e)
        {
            //Some  Code
        }


        #endregion

包含网格的视图:

 <DataGrid  ItemsSource="{Binding Subsystems,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                   SelectedItem="{Binding Path=SelectedSubsystem, Mode=TwoWay}"                        
                   Name="SubsystemAllDataGrid"              
                   Style="{StaticResource DataGridStyle}"
                   Grid.Row="2">

            <DataGrid.Columns>
                <DataGridTextColumn Header="Serial" Binding="{Binding Path=Serial, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Type" Binding="{Binding Path=Type, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="System" Binding="{Binding Path=System, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="SubsystemNo"  Binding="{Binding Path=SubsystemNo, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Description" Binding="{Binding Path=Description, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Scope" Binding="{Binding Path=Scope, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Area" Binding="{Binding Path=Area, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Priority" Binding="{Binding Path=Priority, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="DossierLocation" Binding="{Binding Path=DossierLocation, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Parts" Binding="{Binding Path=Parts, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Status" Binding="{Binding Path=Status, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="StatusDate" Binding="{Binding Path=StatusDate, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="MCDate" Binding="{Binding Path=MCDate, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="PlnMCDate" Binding="{Binding Path=PlnMCDate, Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Remark" Binding="{Binding Path=Remark, Mode=TwoWay}"></DataGridTextColumn>

            </DataGrid.Columns>

        </DataGrid>

问题是: 当我在数据网格的一个列标题的文本框中输入过滤器文本时,没有任何事情发生,并且未点击以下几点的断点:

1-GetTextChangedCommand和SetTextChangedCommand

2-CommandChanged()方法。

我是wpf的新手,所以我确定WPF或C#代码有错误,所以请帮我修复这些错误。

注意:我不使用背后的代码。

提前致谢

2 个答案:

答案 0 :(得分:0)

看起来你的Command-Binding不起作用。请尝试以下方法:

Command="{Binding Path=TextChangedCommand}"

Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}} Path=DataContext.TextChangedCommand}"

答案 1 :(得分:0)

没有命中的断点会让我遇到绑定问题。

您需要将datacontext for you命令设置为定义命令的位置。因此,如果在viewmodel中定义了命令,则需要使用以下命令,但将ansestor类型设置为绑定到viewmodel的对象类型。这通常是您的视图,而不是datagrid:

 Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ViewType}} Path=DataContext.TextChangedCommand}"

因此,如果您的视图是窗口或用户控件,则将ViewType更改为相应的类型。

您还可以在运行应用时查看输出窗口中是否存在为您的命令生成的任何绑定错误。

编辑1: 不要使用依赖属性,在视图模型中使用MVVM-Light中的relay命令:

 this.YourRelayCommand = new RelayCommand( ExecuteYourCommand, CanYourCommandExecute);

  public RelayCommand YourRelayCommand{ get; private set; }

  public bool CanYourCommandExecute() {

    }

  public void ExecuteYourCommand() {
    //TODO: Do code here 
    }