WPF实现支持Command绑定的ComboBox控件
由于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事件!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统