完善主体资料,免费赠送VIP会员!
* 主体类型
* 企业名称
* 信用代码
* 所在行业
* 企业规模
* 所在职位
* 姓名
* 所在行业
* 学历
* 工作性质
请先选择行业
您还可以选择以下福利:
行业福利,领完即止!

下载app免费领取会员

NULL

5cdd2dc095060.jpg

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

发布于:2019-07-25 15:07:29

网友投稿

更多

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

本文版权归腿腿教学网及原创作者所有,未经授权,谢绝转载。

未标题-1.jpg

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

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

60acb4e0ef112.png