wpf 事件参数 绑定到viewmdoel

Posted nocanstillbb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wpf 事件参数 绑定到viewmdoel相关的知识,希望对你有一定的参考价值。

public sealed class EventCommand : TriggerAction<DependencyObject>
    {

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(EventCommand), null);


        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
            "Command", typeof(ICommand), typeof(EventCommand), null);


        public static readonly DependencyProperty InvokeParameterProperty = DependencyProperty.Register(
            "InvokeParameter", typeof(object), typeof(EventCommand), null);

        private string commandName;

        public object InvokeParameter
        {
            get
            {
                return this.GetValue(InvokeParameterProperty);
            }
            set
            {
                this.SetValue(InvokeParameterProperty, value);
            }
        }


        public ICommand Command
        {
            get
            {
                return (ICommand)this.GetValue(CommandProperty);
            }
            set
            {
                this.SetValue(CommandProperty, value);
            }
        }

        public string CommandName
        {
            get
            {
                return this.commandName;
            }
            set
            {
                if (this.CommandName != value)
                {
                    this.commandName = value;
                }
            }
        }

        public object CommandParameter
        {
            get
            {
                return this.GetValue(CommandParameterProperty);
            }

            set
            {
                this.SetValue(CommandParameterProperty, value);
            }
        }

        public object Sender { get; set; }

        protected override void Invoke(object parameter)
        {
            this.InvokeParameter = parameter;
            if (this.AssociatedObject != null)
            {
                ICommand command = this.Command;
                if ((command != null) && command.CanExecute(this.CommandParameter))
                {
                    command.Execute(this.CommandParameter);
                }
            }
        }
    }
<i:Interaction.Triggers>
    <i:EventTrigger EventName="Drop">
        <cmds:EventCommand CommandName="DropCommand" 
        CommandParameter="{Binding RelativeSource={RelativeSource Self},         Path=InvokeParameter}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>    

  

以上是关于wpf 事件参数 绑定到viewmdoel的主要内容,如果未能解决你的问题,请参考以下文章

WPF Window 自定义事件创建和绑定到 ICommand

WPF 事件绑定到 ViewModel(对于非命令类)

WPF 将 UI 事件绑定到 ViewModel 中的命令

WPF数据模板中绑定事件不触发问题

WPF:MVVM模式下ViewModel关闭View

是否有可能看到我在C#WPF中按下数据网格上的向下箭头如何将事件绑定到该?