【问题标题】:AttachedCommandBehaviour MouseEventargs as commandParameterAttachedCommandBehaviour MouseEventargs 作为 commandParameter
【发布时间】:2012-12-06 15:13:38
【问题描述】:

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

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

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    您需要为此使用 Blend 并使用交互触发器执行类似的操作。在触发 Execute 委托时,在您的命令参数中将出现 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
    }
    

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

    【讨论】:

    • 我知道这种方式,但我没有 System.Windows.Interactivity.dll 并且不允许在我的项目中添加新的 .dll,因此我无法使用它。我尝试使用 AttachedCommandBehaviour 来做到这一点。但我认为这是不可能的。
    猜你喜欢
    • 1970-01-01
    • 2013-08-20
    • 2020-01-13
    • 1970-01-01
    • 2014-04-22
    • 2011-01-20
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    相关资源
    最近更新 更多