首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >作为commandParameter的AttachedCommandBehaviour MouseEventargs

作为commandParameter的AttachedCommandBehaviour MouseEventargs
EN

Stack Overflow用户
提问于 2012-12-06 23:13:39
回答 1查看 1.2K关注 0票数 2

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

代码语言:javascript
复制
acb:CommandBehavior.Event="MouseDoubleClick" acb:CommandBehavior.Command="{Binding Command}"
acb:CommandBehavior.CommandParameter="{???}"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-07 05:22:30

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

代码语言:javascript
复制
 <Button>
<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseDoubleClick" >
         <cmd:EventToCommand Command="{Binding MouseDoubleClickCommand}"
             PassEventArgsToCommand="True" />
    </i:EventTrigger>
</i:Interaction.Triggers>

如果您不能使用交互性dll,请尝试以下命令。

代码语言:javascript
复制
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
}

并使用this - MouseDoubleClickBehavior.DoubleClickCommand="{Binding SomeCommand进行绑定}“

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13746630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档