【问题标题】:MvvmLight EventToCommand and WPFToolkit DataGrid double-clickMvvmLight EventToCommand 和 WPFToolkit DataGrid 双击
【发布时间】:2023-03-03 01:38:01
【问题描述】:

试图弄清楚如何使用 EventToCommand 为行设置数据网格双击处理程序。该命令位于每一行的视图模型中。只是我的经验,因为我还没有使用过交互。

谢谢。

我会使用 mvvmlight 标签,但我还没有足够高的代表来制作新标签。

【问题讨论】:

  • 我为你添加了 mvvm-light 标签。这是用于 Laurent Bugnion 的 MVVM Light 工具包的官方版本。

标签: c# mvvm mvvm-light wpfdatagrid wpftoolkit


【解决方案1】:

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

    <Window...    
         ...xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" 
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
            xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras">
         <dg:DataGrid x:Name="dg">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDoubleClick">
                            <GalaSoft_MvvmLight_Command:EventToCommand CommandParameter="{Binding SelectedItem, ElementName=dg}" Command="{Binding Path=SelectCommand, Mode=OneWay}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
            </dg:DataGrid>
    </Window>

您可以创建一个 rowview,因为该行也有自己的视图模型,并在 rowview 中使用该行(容器)的子元素的 mousedoubleclick 事件。

或者你为你的命令绑定创建一个转换器:

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

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

【讨论】:

  • 撕裂...我很长一段时间都没有机会尝试这个,但不想让它无人问津。它在理论上看起来是正确的,但我真的想要命令在 RowViewModel 上的东西。 或者您可能在暗示 DataGrid 不提供支持识别所涉及行的双击?
  • 转换器背后的想法是,转换器转换 SelectedItem 并返回命令。这样,您将在 RowViewModel 上调用命令(我不确定您是否需要它,或者您是否可以执行 SelectedItem.SelectCommand 之类的操作)要确定是否双击了行,您需要 EventArgs。但是您可以使用 mvvm-light 工具包将其返回。
  • K,感谢您提供的示例语法。如果我有时间尝试这种方法,我会再次回复。
【解决方案2】:

如果有人来看这里并想知道我最终是如何在没有 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>

【讨论】:

  • 刚看到这个,没有花太多时间看细节:你不是真的只是在这里实现一个EventToCommand Light吗?
  • 不知道我是否可以这样称呼它,因为这是非常特定于双击的,但是可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
  • 2013-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-31
相关资源
最近更新 更多