c# - AttachedCommandBehaviour MouseEventargs 作为命令参数

标签 c# wpf mvvm

我看到了这个question我有同样的问题,但我在 View 模型中有命令。我可以像 CommandParameter 那样传递 MouseEventargs 吗?如何传递?

acb:CommandBehavior.Event="MouseDoubleClick" acb:CommandBehavior.Command="{Binding Command}"
acb:CommandBehavior.CommandParameter="{???}"

最佳答案

您需要为此使用 Blend,并使用交互触发器执行类似的操作。在您的命令参数中,当执行委托(delegate)被触发时,Mouse EventArgs 将出现。

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

如果你不能使用交互 dll 试试这个..

public static class MouseDoubleClickBehavior
{
    /// <summary>
    /// Hooks up a weak event against the source Selectors MouseDoubleClick
    /// if the Selector has asked for the HandleDoubleClick to be handled
    ///�
    /// If the source Selector has expressed an interest in not having its
    /// MouseDoubleClick handled the internal reference
    /// </summary>
    private static void OnHandleDoubleClickCommandChanged(DependencyObject d,
        DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement ele = d as FrameworkElement;
        if (ele != null)
        {
            ele.MouseLeftButtonDown -= OnMouseDoubleClick;
            if (e.NewValue != null)
            {
                ele.MouseLeftButtonDown += OnMouseDoubleClick;
            }
        }
    }

    /// <summary>
    /// TheCommandToRun : The actual ICommand to run
    /// </summary>
    public static readonly DependencyProperty DoubleClickCommandProperty =
        DependencyProperty.RegisterAttached("DoubleClickCommand",
            typeof(ICommand),
            typeof(MouseDoubleClickBehavior),
            new FrameworkPropertyMetadata((ICommand)null,
                new PropertyChangedCallback(OnHandleDoubleClickCommandChanged)));

    /// <summary>
    /// Gets the TheCommandToRun property. �
    /// </summary>
    public static ICommand GetDoubleClickCommand(DependencyObject d)
    {
        return (ICommand)d.GetValue(DoubleClickCommandProperty);
    }

    /// <summary>
    /// Sets the TheCommandToRun property. �
    /// </summary>
    public static void SetDoubleClickCommand(DependencyObject d, ICommand value)
    {
        d.SetValue(DoubleClickCommandProperty, value);
    }

    #region Handle the event

    /// <summary>
    /// Invoke the command we tagged.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private static void OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        //check for double clicks.
        if (e.ClickCount != 2)
            return;
        FrameworkElement ele = sender as FrameworkElement;

        DependencyObject originalSender = e.OriginalSource as DependencyObject;
        if (ele == null || originalSender == null)
            return;

        ICommand command = (ICommand)(sender as DependencyObject).GetValue(DoubleClickCommandProperty);

        if (command != null)
        {
            if (command.CanExecute(null))
                command.Execute(null);
        }
    }
    #endregion
}

并使用此绑定(bind) - MouseDoubleClickBehavior.DoubleClickCommand="{Binding SomeCommand}"

关于c# - AttachedCommandBehaviour MouseEventargs 作为命令参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13746630/

相关文章:

.net - 编辑对话框,带有 WPF 中的绑定(bind)和确定/取消

.net - 默认上下文菜单样式 - WPF

c# - TreeView 的 PropertyChanged 始终为 null

wpf - TreeView 上的 MvvmLight EventToCommand 抛出 NullReferenceException

wpf - WPF中计算器的MVVM代码

c# - 如何获取引发 UnauthorizedAccessException 的路径?

c# - 如何在 Azure Functions 中处理来自多个服务总线队列消息触发器的数据库连接?

c# - 如何使用 OpenXML 获取 Excel 工作表的使用范围?

c# - WPF 中的同步/阻塞动画?

c# - Application.Idle事件意义