Question

Essayer de comprendre comment utiliser EventToCommand pour définir un gestionnaire double clic DataGrid pour les lignes. La commande vit dans la viewmodel pour chaque ligne. Il suffit de que beaucoup de mon expérience, puisque je ne l'ai pas encore utilisé les interactions.

Merci.

Je l'ai utilisé tag mvvmlight, mais je n'ai pas encore assez haut représentant pour faire de nouvelles balises.

Était-ce utile?

La solution

Ce serait la solution si la commande vit sur la « GridVieModel » et non sur « 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>

Vous pouvez créer un rowview depuis la ligne a également son propre viewmodel et utiliser l'événement MouseDoubleClick d'un élément enfant de la ligne (conteneur) dans le rowview.

Ou vous créez un convertisseur pour votre commande de liaison:

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

Le convertisseur alors vérifier si le selectedItem est du type requis pour retourner la commande (Quelque chose comme ISelectCommandable avec une propriété RelayCommand)

Autres conseils

Dans le cas où quelqu'un vient regarder ici et se demande comment je fini par faire w / o 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
}

Dans mon 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>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top