Search code examples
wpfmvvmcommand-behaviors

Command Behavior Attachments Arguments


I have implemented the code in this article: http://wblo.gs/YvF. Everything works as expected but I don't know how to get access to the Arguments passed into the event handler when the event fires. I know there is the command parameter but how do I use it to get access to the EventArgs? Here is the code I have implemented...

DragEnter Class

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

public class DragEnter : Attachment<Control, DragEnterBehavior, DragEnter>
{
    private static readonly DependencyProperty behaviorProperty = Behavior();
    public static readonly DependencyProperty CommandProperty = Command(behaviorProperty);
    public static readonly DependencyProperty CommandParameterProperty = Parameter(behaviorProperty);

    public static void SetCommand(Control control, ICommand command) { control.SetValue(CommandProperty, command); }
    public static ICommand GetCommand(Control control) { return control.GetValue(CommandProperty) as ICommand; }
    public static void SetCommandParameter(Control control, object parameter) { control.SetValue(CommandParameterProperty, parameter); }
    public static object GetCommandParameter(Control buttonBase) { return buttonBase.GetValue(CommandParameterProperty); }
}

DragEnterBehavior Class

using System.Windows.Controls;
using Microsoft.Practices.Prism.Commands;

public class DragEnterBehavior : CommandBehaviorBase<Control>
{
    public DragEnterBehavior(Control selectableObject)
        : base(selectableObject)
    {
        selectableObject.DragEnter += (sender, args) => ExecuteCommand();
    }
}

The Implementation Code

public ICommand EditItemCommand
{
    get
    {
        if (editItemCommand == null)
            editItemCommand = new RelayCommand(param => EditItemControl(), pre => IsItemEditButtonEnabled());

        return editItemCommand;
    }
}

public void EditItemControl()
{
    ...
    ChangedView(itemEditorViewModel);
}

<ListBox Behaviors:DragEnter.Command="{Binding EditItemCommand}" ...

Any help on this would be appreciated. Thank You!


Solution

  • This is untested, but

    • Change your DragEnterBehaviour to

      public class DragEnterBehavior : CommandBehaviorBase<Control>
      {
          public DragEnterBehavior(Control selectableObject)
              : base(selectableObject)
          {
              selectableObject.DragEnter += (sender, args) =>
                                            {
                                                CommandParameter = args;
                                                ExecuteCommand();
                                            };
          }
      } 
      
    • Change your RelayCommand (I'm assuming this is from MVVM Light) to

      public ICommand EditItemCommand 
      { 
          get 
          { 
              if (editItemCommand == null) 
                  editItemCommand = new RelayCommand<DragEventArgs>(
                      EditItemControl, IsItemEditButtonEnabled); 
      
              return editItemCommand; 
          } 
      } 
      
      public void EditItemControl(DragEventArgs args) 
      { 
          ... 
      } 
      

    If you are using MVVM Light then you achieve the same result like this:

    <i:Interaction.Triggers> 
        <i:EventTrigger EventName="DragEnter"> 
            <cmd:EventToCommand Command="{Binding DragEnterCommand}" 
                PassEventArgsToCommand="True" /> 
        </i:EventTrigger> 
    </i:Interaction.Triggers>