WPF扩展ComboBox实现Command Binding事件

参考文章:https://www.cnblogs.com/xiwang/archive/2012/07/23/2604924.html 

public class ComboBoxEx:ComboBox,ICommandSource
    {
        private static EventHandler _canExecuteChangeHandler;

        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command",
            typeof(ICommand), typeof(ComboBoxEx),
            new PropertyMetadata((ICommand) null, new PropertyChangedCallback(CommandChanged)));

        /// <summary>获取在调用命令源时执行的命令。</summary>
        /// <returns>在调用命令源时执行的命令。</returns>
        public ICommand Command {
            get { return (ICommand) GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

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


        /// <summary>表示可在执行命令时传递给该命令的用户定义的数据值。</summary>
        /// <returns>命令特定数据。</returns>
        public object CommandParameter
        {
            get { return (object) GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }

        }

        public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget",
            typeof(IInputElement), typeof(ComboBoxEx), new PropertyMetadata((IInputElement)null));

        /// <summary>在其上执行该命令的对象。</summary>
        /// <returns>在其上执行该命令的对象。</returns>
        public IInputElement CommandTarget
        {
            get { return (IInputElement) GetValue(CommandTargetProperty); }
            set { SetValue(CommandTargetProperty, value); }
        }

        public ComboBoxEx() : base()
        {

        }

        private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ComboBoxEx cb = (ComboBoxEx) d;
            cb.HookUpCommand((ICommand) e.OldValue, (ICommand) e.NewValue);
        }

        private void HookUpCommand(ICommand oldCommand, ICommand newCommand)
        {
            if (null != oldCommand)
            {
                RemoveCommand(oldCommand, newCommand);
            }

            AddCommand(oldCommand, newCommand);
        }

        private void RemoveCommand(ICommand oldCommand, ICommand newCommand)
        {
            EventHandler handler = CanExecuteChanged;
            oldCommand.CanExecuteChanged -= handler;
        }

        private void AddCommand(ICommand oldCommand, ICommand newCommand)
        {
            EventHandler handler = new EventHandler(CanExecuteChanged);
            _canExecuteChangeHandler = handler;
            if (null!= newCommand)
            {
                newCommand.CanExecuteChanged += _canExecuteChangeHandler;

            }
        }

        private void CanExecuteChanged(object sender, EventArgs e)
        {
            if (null != this.Command)
            {
                RoutedCommand command = this.Command as RoutedCommand;

                // if a RoutedCommand
                this.IsEnabled = null != command ? command.CanExecute(this.CommandParameter, this.CommandTarget) : Command.CanExecute(CommandParameter);
            }
        }

        /// <summary>
        ///   响应 <see cref="T:System.Windows.Controls.ComboBox" /> 通过引发选择更改 <see cref="E:System.Windows.Controls.Primitives.Selector.SelectionChanged" /> 事件。
        /// </summary>
        /// <param name="e">
        ///   将提供数据供 <see cref="T:System.Windows.Controls.SelectionChangedEventArgs" />。
        /// </param>
        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            base.OnSelectionChanged(e);

            if (null != this.Command)
            {
                RoutedCommand command = this.Command as RoutedCommand;

                if (null != command)
                {
                    command.Execute(this.CommandParameter, this.CommandTarget);
                }
                else
                {
                    ((ICommand) Command).Execute(CommandParameter);
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_42930928/article/details/87859585