WPF 给控件添加可以绑定的命令


在WPF里的Button有一个可以绑定的Command的属性,只要绑定好这个属性以后,只要你ClickButton就

会运行这个命令,但这时我们可以考虑一下这个问题,为什么是Click来触发呢?为什么不是右键单击来触发呢,

下面研究一下,怎么能写一个右键单机能触发的命令:

首先现有的Button肯定是不行了,所以自己写一个TButton ,它继承自Button

    public class TButton:Button
    {
        public static readonly DependencyProperty TCommandParameterProperty = DependencyProperty.Register("TCommandParameter", typeof(object), typeof(TButton));
        public static readonly DependencyProperty TCommandProperty = DependencyProperty.Register("TCommand", typeof(ICommand), typeof(TButton));
        public static readonly DependencyProperty TCommandTargetProperty = DependencyProperty.Register("TCommandTarget", typeof(object), typeof(TButton));
        public ICommand TCommand
        {
            get
            {
                return (ICommand)GetValue(TCommandProperty);
            }
            set
            {
                SetValue(TCommandProperty, value);
            }
        }
        public object TCommandParameter
        {
            get
            {
                return GetValue(TCommandParameterProperty);
            }
            set
            {
                SetValue(TCommandParameterProperty, value);
            }
        }
        public IInputElement TCommandTarget
        {
            get
            {
                return (IInputElement)GetValue(TCommandTargetProperty);
            }
            set
            {
                SetValue(TCommandTargetProperty, value);
            }
        }

        protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseRightButtonUp(e);
            RoutedCommand rcmd = TCommand as RoutedCommand;
            if(rcmd!=null)
            {
                if(rcmd.CanExecute(TCommandParameter,TCommandTarget))
                {
                    rcmd.Execute(TCommandParameter, TCommandTarget);
                }                
            }
            else
            {
                if(TCommand!=null)
                {
                    if(TCommand.CanExecute(TCommandParameter))
                    {
                        TCommand.Execute(TCommandParameter);
                    }
                }
            }
        }
    }

再写一个命令

    public class TCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;
        public bool CanExecute(object parameter)
        {
            return true;
        }
        public void Execute(object parameter)
        {
            Window win = parameter as Window;
            if (win != null)
                win.Close();
        }
    }

再界面里绑定:

        <local:TButton x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="411,277,0,0" VerticalAlignment="Top" Width="75" TCommand="{Binding TCommand}" TCommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>

测试可以

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值