首页/技术分享

二次开发教程:WPF 给控件添加可以绑定的命令

发布于:2019-07-25 15:07:29
4362人 分享

在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}}}"/>

转载请注明来源本文地址:https://www.tuituisoft/blog/3187.html

上一篇:

二次开发教程:WPF DataContent内存释放问题

下一篇:

二次开发教程:WPF 依赖属性