WPF實現支援Command綁定的ComboBox控制項

來源:互聯網
上載者:User

由於ComboBox,ListBox等控制項沒有實現ICommandSource介面,所以不支援在XAML中進行Command綁定,下面的一段代碼就是,對ComboBox實現對ICommandSource介面的實現:

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);            }        }    }}

如此,便可以在XAML中對Command進行綁定,處理ComboBox的SelectedChanged事件!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.