WPF supports ComboBox controls bound to commands

Source: Internet
Author: User

The ComboBox and ListBox controls do not implement the icommandsource interface, so command binding in XAML is not supported. The following code implements the icommandsource interface for ComboBox:

public class ComboBoxWithCommand : ComboBox, ICommandSource{    private static EventHandler canExecuteChangedHandler;    public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command",                                                                                            typeof(ICommand),                                                                                            typeof(ComboBoxWithCommand),                                                                                            new PropertyMetadata((ICommand)null,                                                                                            new PropertyChangedCallback(CommandChanged)));    public ICommand Command    {        get        {            return (ICommand)GetValue(CommandProperty);        }        set        {            SetValue(CommandProperty, value);        }    }    public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget",                                                                                                  typeof(IInputElement),                                                                                                  typeof(ComboBoxWithCommand),                                                                                                  new PropertyMetadata((IInputElement)null));    public IInputElement CommandTarget    {        get        {            return (IInputElement)GetValue(CommandTargetProperty);        }        set        {            SetValue(CommandTargetProperty, value);        }    }    public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter",                                                                                                     typeof(object),                                                                                                     typeof(ComboBoxWithCommand),                                                                                                     new PropertyMetadata((object)null));    public object CommandParameter    {        get        {            return (object)GetValue(CommandParameterProperty);        }        set        {            SetValue(CommandParameterProperty, value);        }    }    public ComboBoxWithCommand() : base() { }    private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)    {        ComboBoxWithCommand cb = (ComboBoxWithCommand)d;        cb.HookUpCommand((ICommand)e.OldValue, (ICommand)e.NewValue);    }    private void HookUpCommand(ICommand oldCommand, ICommand newCommand)    {        if (oldCommand != null)        {            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);        canExecuteChangedHandler = handler;        if (newCommand != null)        {            newCommand.CanExecuteChanged  = canExecuteChangedHandler;        }    }    private void CanExecuteChanged(object sender, EventArgs e)    {        if (this.Command != null)        {            RoutedCommand command = this.Command as RoutedCommand;            // If a RoutedCommand.            if (command != null)            {                if (command.CanExecute(this.CommandParameter, this.CommandTarget))                {                    this.IsEnabled = true;                }                else                {                    this.IsEnabled = false;                }            }            // If a not RoutedCommand.            else            {                if (Command.CanExecute(CommandParameter))                {                    this.IsEnabled = true;                }                else                {                    this.IsEnabled = false;                }            }        }    }    protected override void OnSelectionChanged(SelectionChangedEventArgs e)    {        base.OnSelectionChanged(e);        if (this.Command != null)        {            RoutedCommand command = this.Command as RoutedCommand;            if (command != null)            {                command.Execute(this.CommandParameter, this.CommandTarget);            }            else            {                ((ICommand)Command).Execute(CommandParameter);            }        }    }}

In this way, you can bind the command in XAML to process the selectedchanged event of ComboBox!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.