1

我已经实现了这篇文章中的代码:http ://wblo.gs/YvF 。一切都按预期工作,但我不知道如何在事件触发时访问传递给事件处理程序的参数。我知道有命令参数,但我如何使用它来访问 EventArgs?这是我实现的代码...

拖动输入类

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 类

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();
    }
}

实施代码

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}" ...

对此的任何帮助将不胜感激。谢谢你!

4

1 回答 1

1

这是未经测试的,但是

  • 将您的 DragEnterBehaviour 更改为

    public class DragEnterBehavior : CommandBehaviorBase<Control>
    {
        public DragEnterBehavior(Control selectableObject)
            : base(selectableObject)
        {
            selectableObject.DragEnter += (sender, args) =>
                                          {
                                              CommandParameter = args;
                                              ExecuteCommand();
                                          };
        }
    } 
    
  • 将您的 RelayCommand(我假设这是来自 MVVM Light)更改为

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

如果您使用的是 MVVM Light,那么您将获得相同的结果,如下所示:

<i:Interaction.Triggers> 
    <i:EventTrigger EventName="DragEnter"> 
        <cmd:EventToCommand Command="{Binding DragEnterCommand}" 
            PassEventArgsToCommand="True" /> 
    </i:EventTrigger> 
</i:Interaction.Triggers> 
于 2012-05-28T16:41:53.973 回答