• WPF命令绑定捕获命令参数eventArgs


    定义delegateCommand:

    public class DelegateCommand<T> : ICommand
        {
            private readonly Predicate<T> _canExecute;
            private readonly Action<T> _execute;
    
    
            public DelegateCommand(Action<T> execute) : this(execute, null)
            {
            }
    
            public DelegateCommand(Action<T> execute, Predicate<T> canExecute)
            {
                if (execute == null)
                {
                    throw new ArgumentNullException("execute");
                }
    
                _execute = execute;
                _canExecute = canExecute;
            }
    
            #region ICommand Members
    
            public bool CanExecute(object parameter)
            {
                return _canExecute == null ? true : _canExecute((T)parameter);
            }
    
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }
    
            public void Execute(object parameter)
            {
                _execute((T)parameter);
            }
    
            #endregion
        }

    定义eventTriggerAction :

     public class EventTriggerAction : TriggerAction<DependencyObject>
        {
            protected override void Invoke(object parameter)
            {
                if (CommandParameter != null)
                {
                    Command?.Execute(CommandParameter);
                }
                else
                {
                    Command?.Execute(parameter);
                }
            }
    
            // Using a DependencyProperty as the backing store for Command.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty CommandProperty =
                DependencyProperty.Register("Command", typeof(ICommand), typeof(EventTriggerAction),
                    new PropertyMetadata(null));
    
    
            public ICommand Command
            {
                get { return (ICommand)GetValue(CommandProperty); }
                set { SetValue(CommandProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for CommandParameter.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty CommandParameterProperty =
                DependencyProperty.Register("CommandParameter", typeof(object), typeof(EventTriggerAction),
                    new PropertyMetadata(null));
    
    
            public object CommandParameter
            {
                get { return GetValue(CommandParameterProperty); }
                set { SetValue(CommandParameterProperty, value); }
            }
        }

    前台绑定:

       <i:Interaction.Triggers>
            <i:EventTrigger   EventName="KeyDown">
                <local:EventTriggerAction  Command="{Binding KeyDownCMD}"></local:EventTriggerAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>

    源码:https://files.cnblogs.com/files/lizhijian/2021-01-04-WPF-CommandWithParameter.rar

  • 相关阅读:
    读书笔记Review: HTTP and HttpServletRequest
    读书笔记JavaScript Patterns_chapter6_Code Reuse Patterns
    读书笔记Review: servlet lifecycle and API
    读书笔记Review: HttpServletResponse
    简单分析Ext.ComponentMgr 函数
    翻译 Extjs in action中的Event flow in the DOM
    struts中的web.xml
    读书笔记_Extjs In Action_The Component Life Cycle
    web service中的事件
    Remoting学习笔记
  • 原文地址:https://www.cnblogs.com/congqiandehoulai/p/14262355.html
Copyright © 2020-2023  润新知