WPF DataGrid双击命令+ INotifyPropertyChanged?

时间:2011-11-30 13:59:33

标签: c# wpf mvvm

我想在单击DataGrid项目时执行ViewModel中的命令。作为参数,我希望有相应的行。

我在互联网上找到了一种方法,但它使用了DependencyProperty

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/632ea875-a5b8-4d47-85b3-b30f28e0b827

我在项目中不使用DependencyProperty,而是使用INotifyPropertyChanged。如何在不使用DependencyProperty

的情况下实现“双击数据网格”commaind

4 个答案:

答案 0 :(得分:8)

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
...
<DataGrid SelectedItem={Binding MySelectedItem, Mode=TwoWay}>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseDoubleClick">
        <i:InvokeCommandAction Command="{Binding YourCommand}" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</DataGrid>

答案 1 :(得分:3)

我通常使用AttachedCommandBehavior。它是3个类文件,可以添加到您的项目中,并允许您将命令附加到任何事件。

以下是如何使用它的示例:

<Style TargetType="{x:Type DataGridRow}">
    <Setter Property="local:CommandBehavior.Event" Value="MouseDoubleClick" />
    <Setter Property="local:CommandBehavior.Action" Value="{Binding MyDoubleClickCommand}" />
    <Setter Property="local:CommandBehavior.CommandParameter" Value="{Binding }" />
</Style>

答案 2 :(得分:2)

MVVM Light Toolkit提供了EventToCommand行为,这应该能够实现所需的行为(如果你不想使用框架,你总是可以自己动手)。

答案 3 :(得分:0)

您可以使用this code-behind代码段来识别双击的行。

在注释“//对基础数据执行某些操作的行中,您可以从Grid或行的DataContext获取附加的ViewModel,并以行作为参数调用您的Command。

相关问题