zoukankan      html  css  js  c++  java
  • 利用System.Windows.Interactivity.dll实现View时间在ViewModel中处理(可带sender、EventArgs参数)

    一、公共类
    namespace Gh.Ticket.Common

    {

        public class ExtendedCommandParameter

        {

            public ExtendedCommandParameter(EventArgs eventArgs, FrameworkElement sender, object parameter)

            {

                EventArgs = eventArgs;

                Sender = sender;

                Parameter = parameter;

            }

            public EventArgs EventArgs { getprivate set; }

            public FrameworkElement Sender { getprivate set; }

            public object Parameter { getprivate set; }

        }

        public class ExtendedInvokeCommandAction : TriggerAction<FrameworkElement>

        {

            public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command"typeof(ICommand), typeof(ExtendedInvokeCommandAction), new PropertyMetadata(null, CommandChangedCallback));

            public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter"typeof(object), typeof(ExtendedInvokeCommandAction), new PropertyMetadata(null, CommandParameterChangedCallback));

            private static void CommandParameterChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)

            {

                var invokeCommand = d as ExtendedInvokeCommandAction;

                if (invokeCommand != null)

                    invokeCommand.SetValue(CommandParameterProperty, e.NewValue);

            }

            private static void CommandChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)

            {

                var invokeCommand = d as ExtendedInvokeCommandAction;

                if (invokeCommand != null)

                    invokeCommand.SetValue(CommandProperty, e.NewValue);

            }

            protected override void Invoke(object parameter)

            {

                if (this.Command == null)

                    return;

                if (this.Command.CanExecute(parameter))

                {

                    var commandParameter = new ExtendedCommandParameter(parameter as EventArgsthis.AssociatedObject,

                                                                        GetValue(CommandParameterProperty));

                    this.Command.Execute(commandParameter);

                }

            }

            #region public properties

            public object CommandParameter

            {

                get { return GetValue(CommandParameterProperty); }

                set { SetValue(CommandParameterProperty, value); }

            }

            public ICommand Command

            {

                get { return GetValue(CommandProperty) as ICommand; }

                set { SetValue(CommandParameterProperty, value); }

            }

            #endregion

        }

    }

    二、View

     xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

     xmlns:local="ExtendedInvokeCommandAction类所在命名空间"

      <TextBox x:Name="txtControlID"  >

                            <i:Interaction.Triggers>

                                <i:EventTrigger EventName="PreviewKeyDown">

                                    <local:ExtendedInvokeCommandAction

                                            Command="{Binding CustomCommand}"

                                            //这里不绑定则ExtendedCommandParameter.Parameter的值为对应ViewModel的值,否则可绑定其他控件的属性值

                                            CommandParameter="{Binding}"

                                    />

                                </i:EventTrigger>

                            </i:Interaction.Triggers>

                        </TextBox>

    三、ViewMode
     public ICommand CustomCommand

            {

                get

                {

                    return new DelegateCommand<object>(x =>

                        {

                            ExtendedCommandParameter p = x as ExtendedCommandParameter;

                             p.sender  触发事件对象

                             p.EventArgs 事件参数                                       

                        });

                }

            }

  • 相关阅读:
    Python pip 下载速度慢? Windows 设置 国内源,用阿里云国内镜像加速
    Go timer 是如何被调度的?
    Go sync.Pool 浅析
    一次错误使用 go-cache 导致出现的线上问题
    golang面向对象分析
    一文完全掌握 Go math/rand
    这一次,彻底搞懂 Go Cond
    面试题:让你捉摸不透的 Go reslice
    当 Go struct 遇上 Mutex
    这可能是最容易理解的 Go Mutex 源码剖析
  • 原文地址:https://www.cnblogs.com/gossip/p/2098255.html
Copyright © 2011-2022 走看看