C# MvvmLight EventToCommand和WPFToolkit DataGrid双击

C# MvvmLight EventToCommand和WPFToolkit DataGrid双击,c#,mvvm,mvvm-light,wpfdatagrid,wpftoolkit,C#,Mvvm,Mvvm Light,Wpfdatagrid,Wpftoolkit,正在尝试了解如何使用EventToCommand为行设置datagrid双击处理程序。命令位于每行的viewmodel中。这就是我的经验,因为我还没有使用交互 谢谢 我本来会使用mvvmlight标记,但我还没有足够高的rep来创建新标记。如果命令位于“GridVieModel”而不是“RowViewModel”上,这将是解决方案 您可以创建rowview,因为该行也有自己的viewmodel,并在rowview中使用该行(容器)的子元素的mousedoubleclick事件 或者为命令绑定

正在尝试了解如何使用EventToCommand为行设置datagrid双击处理程序。命令位于每行的viewmodel中。这就是我的经验,因为我还没有使用交互

谢谢


我本来会使用mvvmlight标记,但我还没有足够高的rep来创建新标记。

如果命令位于“GridVieModel”而不是“RowViewModel”上,这将是解决方案


您可以创建rowview,因为该行也有自己的viewmodel,并在rowview中使用该行(容器)的子元素的mousedoubleclick事件

或者为命令绑定创建转换器:

<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding SelectedItem, ElementName=dg, Mode=OneWay, Converter=...}"/>


然后,转换器将检查selectedItem是否为返回命令所需的类型(类似于带有RelayCommand属性的ISelectCommandable)

以防有人看到这里并想知道我是如何在没有EventToCommand的情况下完成此操作的

public class DataGridAttachedBehaviors
{
   #region DoubleClick

   public static DependencyProperty OnDoubleClickProperty = DependencyProperty.RegisterAttached(
       "OnDoubleClick",
       typeof(ICommand),
       typeof(DataGridAttachedBehaviors),
       new UIPropertyMetadata(DataGridAttachedBehaviors.OnDoubleClick));

   public static void SetOnDoubleClick(DependencyObject target, ICommand value)
   {
      target.SetValue(DataGridAttachedBehaviors.OnDoubleClickProperty, value);
   }

   private static void OnDoubleClick(DependencyObject target, DependencyPropertyChangedEventArgs e)
   {
      var element = target as Control;
      if (element == null)
      {
         throw new InvalidOperationException("This behavior can be attached to a Control item only.");
      }

      if ((e.NewValue != null) && (e.OldValue == null))
      {
         element.MouseDoubleClick += MouseDoubleClick;
      }
      else if ((e.NewValue == null) && (e.OldValue != null))
      {
         element.MouseDoubleClick -= MouseDoubleClick;
      }
   }

   private static void MouseDoubleClick(object sender, MouseButtonEventArgs e)
   {
      UIElement element = (UIElement)sender;
      ICommand command = (ICommand)element.GetValue(DataGridAttachedBehaviors.OnDoubleClickProperty);
      command.Execute(null);
   }

   #endregion DoubleClick

  #region SelectionChanged
  //removed
  #endregion
}
在我的xaml中:

<dg:DataGrid.RowStyle>
   <Style BasedOn="{StaticResource DataGridDemoRowStyle}"           
          TargetType="{x:Type dg:DataGridRow}">
       <Setter Property="skins:DataGridAttachedBehaviors.OnDoubleClick"
               Value="{Binding Recall}" />
   </Style>
</dg:DataGrid.RowStyle>


我为您添加了mvvm灯标记。这是Laurent Bugnon的MVVM Light toolkit的官方版本。撕破了。。。我很长一段时间都没有机会尝试这个,但我不想让它没有答案。理论上看起来是对的,但我真的想要一些命令在RowViewModel上的东西。或者,您可能是在暗示DataGrid不提供支持标识所涉及行的双击功能?转换器背后的想法是,转换器强制转换SelectedItem并返回命令。这样,您就可以调用RowViewModel上的命令(我不确定您是否需要它,或者您是否可以执行类似SelectedItem.SelectCommand的操作)来确定行是否被双击,您将需要EventArgs。但是您可以使用mvvm light toolkit.K将其返回到,谢谢您提供的示例语法。如果我有时间尝试这种方法,我会再次回应。我只是看到了这一点,没有花太多时间看细节:你不是真的只是在这里实现了一个EventToCommand Light吗?我不知道我是否可以这样称呼它,因为这是非常具体的双击,但肯定。
<dg:DataGrid.RowStyle>
   <Style BasedOn="{StaticResource DataGridDemoRowStyle}"           
          TargetType="{x:Type dg:DataGridRow}">
       <Setter Property="skins:DataGridAttachedBehaviors.OnDoubleClick"
               Value="{Binding Recall}" />
   </Style>
</dg:DataGrid.RowStyle>