سؤال

محاولة معرفة كيفية استخدام EventToCommand لتحديد معالج النقر المزدوج DataGrid للصفوف. يعيش الأمر في ViewModel لكل صف. فقط الذي - التي الكثير من تجربتي، لأنني لم أستخدم التفاعلات بعد.

شكرا.

كنت قد استخدمت علامة mvvmlight، لكن ليس لدي منتبرات عالية بما فيه الكفاية لجعل علامات جديدة.

هل كانت مفيدة؟

المحلول

سيكون هذا هو الحل إذا كان الأمر يعيش على "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 نظرا لأن الصف يحتوي أيضا على ViewModel الخاص به واستخدام الحدث Mousedoublicklick من عنصر الطفل في الصف (حاوية) في المجال.

أو يمكنك إنشاء محول لأمرك الربط:

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

ثم سيحقق المحول ما إذا كان SELEVENTITEM من النوع المطلوب لإرجاع الأمر (شيء مثل IselectCommandable مع خاصية RelayCommand)

نصائح أخرى

في حالة وجود أي شخص يبحث هنا ويتساءل كيف انتهى بي الأمر بعمل 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
}

في بلدي 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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top