5

EventToCommandを使用して、行のデータグリッドダブルクリックハンドラーを設定する方法を理解しようとしています。コマンドは、各行のビューモデルに存在します。私はまだインタラクションを使用していないので、私の経験からはそれだけです。

ありがとう。

mvvmlightタグを使用したはずですが、新しいタグを作成するのに十分な担当者がまだいません。

4

2 に答える 2

11

これは、コマンドが「RowViewModel」ではなく「GridVieModel」に存在する場合の解決策になります。

    <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>

行にも独自のビューモデルがあり、行ビューの行(コンテナ)の子要素のmousedoubleclickイベントを使用するため、行ビューを作成できます。

または、コマンドバインディング用のコンバーターを作成します。

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

次に、コンバーターは、selectedItemがコマンドを返すために必要なタイプであるかどうかをチェックします(RelayCommandプロパティを持つISelectCommandableのようなもの)

于 2010-04-23T08:20:30.477 に答える
4

誰かがここを見に来て、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>
于 2010-04-29T16:20:41.800 に答える