WPF_MVVM实现Command绑定获取事件参数EventArgs

这里主要实现的效果是窗体在关闭的时候,需要弹出消息框确认是否关闭,来拦截窗体的Closing事件,无需通过Loaded事件传递控件对象到ViewModel中。

1.创建WPF项目,需要引入System.Windows.Interactivity.dll

2.创建EventToCommand类并继承至TriggerAction<DependencyObject>类,在xaml引入并实现Command绑定,具体实现代码如下:

 public class EventToCommand : TriggerAction<DependencyObject>
    {
        public bool PassEventArgsToCommand { get; set; }

        #region Command
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand),
         typeof(EventToCommand), new PropertyMetadata(null));
        public ICommand Command
        {
            get
            {
                return (ICommand)GetValue(CommandProperty);
            }
            set
            {
                SetValue(CommandProperty, value);
            }
        }
        #endregion

        #region CommandParameter
        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object),
         typeof(EventToCommand), new PropertyMetadata(null));

        public object CommandParameter
        {
            get
            {
             
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zxy644492473

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值