Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 命令行为附件参数_Wpf_Mvvm_Command Behaviors - Fatal编程技术网

Wpf 命令行为附件参数

Wpf 命令行为附件参数,wpf,mvvm,command-behaviors,Wpf,Mvvm,Command Behaviors,我已经实现了本文中的代码:。一切正常,但我不知道如何在事件触发时访问传递到事件处理程序的参数。我知道有命令参数,但如何使用它来访问EventArgs?下面是我已经实现的代码 德拉根特级 using System.Windows; using System.Windows.Controls; using System.Windows.Input; public class DragEnter : Attachment<Control, DragEnterBehavior, DragEnte

我已经实现了本文中的代码:。一切正常,但我不知道如何在事件触发时访问传递到事件处理程序的参数。我知道有命令参数,但如何使用它来访问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); }
}
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Input;
公共级疏水剂:附件
{
私有静态只读依赖项Property behaviorProperty=Behavior();
public static readonly dependencProperty CommandProperty=Command(behaviorProperty);
公共静态只读DependencyProperty命令ParameterProperty=参数(behaviorProperty);
公共静态void SetCommand(控件控件,ICommand命令){Control.SetValue(CommandProperty,command);}
公共静态ICommand GetCommand(控件控件){将Control.GetValue(CommandProperty)返回为ICommand;}
公共静态void SetCommandParameter(控件控件,对象参数){Control.SetValue(CommandParameterProperty,参数);}
公共静态对象GetCommandParameter(控制按钮库){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();
    }
}
使用System.Windows.Controls;
使用Microsoft.Practices.Prism.Commands;
公共类DragEnterBehavior:CommandBehaviorBase
{
公共DragEnterBehavior(控件可选择对象)
:base(selectableObject)
{
selectableObject.DragEnter+=(发送方,参数)=>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}" ...
public ICommand EditItemCommand
{
得到
{
if(editItemCommand==null)
editItemCommand=new RelayCommand(参数=>EditItemControl(),pre=>IsItemEditButtonEnabled());
返回editItem命令;
}
}
公共无效EditItemControl()
{
...
ChangedView(itemEditorViewModel);
}
这是未经测试的,但是

  • 将您的DragEnterBehaviour更改为

    public class DragEnterBehavior : CommandBehaviorBase<Control>
    {
        public DragEnterBehavior(Control selectableObject)
            : base(selectableObject)
        {
            selectableObject.DragEnter += (sender, args) =>
                                          {
                                              CommandParameter = args;
                                              ExecuteCommand();
                                          };
        }
    } 
    
    公共类DragEnterBehavior:CommandBehaviorBase
    {
    公共DragEnterBehavior(控件可选择对象)
    :base(selectableObject)
    {
    selectableObject.DragEnter+=(发送方,参数)=>
    {
    CommandParameter=args;
    ExecuteCommand();
    };
    }
    } 
    
  • 将您的RelayCommand(我假设这是从MVVM灯)更改为

    public ICommand EditItemCommand
    { 
    得到
    { 
    if(editItemCommand==null)
    editItemCommand=新的RelayCommand(
    EditItemControl,IsItemEditButtonEnabled);
    返回editItem命令;
    } 
    } 
    公共无效EditItemControl(DragEventArgs参数)
    { 
    ... 
    } 
    
如果您使用的是MVVM Light,则可以获得如下相同的结果:

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

这未经测试,但

  • 将您的DragEnterBehaviour更改为

    public class DragEnterBehavior : CommandBehaviorBase<Control>
    {
        public DragEnterBehavior(Control selectableObject)
            : base(selectableObject)
        {
            selectableObject.DragEnter += (sender, args) =>
                                          {
                                              CommandParameter = args;
                                              ExecuteCommand();
                                          };
        }
    } 
    
    公共类DragEnterBehavior:CommandBehaviorBase
    {
    公共DragEnterBehavior(控件可选择对象)
    :base(selectableObject)
    {
    selectableObject.DragEnter+=(发送方,参数)=>
    {
    CommandParameter=args;
    ExecuteCommand();
    };
    }
    } 
    
  • 将您的RelayCommand(我假设这是从MVVM灯)更改为

    public ICommand EditItemCommand
    { 
    得到
    { 
    if(editItemCommand==null)
    editItemCommand=新的RelayCommand(
    EditItemControl,IsItemEditButtonEnabled);
    返回editItem命令;
    } 
    } 
    公共无效EditItemControl(DragEventArgs参数)
    { 
    ... 
    } 
    
如果您使用的是MVVM Light,则可以获得如下相同的结果:

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