WPF 中命令绑定的核心是
ICommand接口,它让 UI 元素(如 Button、MenuItem)能“声明式”地触发逻辑,而不直接耦合事件处理代码。关键不是写一堆 Click 事件,而是把“能执行什么”和“什么时候可执行”封装成对象,再通过 XAML 绑定过去。
用 RelayCommand(最常用)快速实现 ICommand
微软没提供默认实现,但社区广泛使用
RelayCommand(也叫
DelegateCommand)——它用两个委托(
Action和
Predicate)分别表示“执行动作”和“是否可用”。你可以自己写一个轻量版:
public class RelayCommand : ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
public RelayCommand(Action execute, Func<bool> canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public bool CanExecute(object parameter) => _canExecute?.Invoke() ?? true;
public void Execute(object parameter) => _execute();
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
}
注意:
CanExecuteChanged事件里用了
CommandManager.RequerySuggested,这是 WPF 自动触发重查“是否可用”的机制(比如焦点切换、按键后),不用手动调用
RaiseCanExecuteChanged()——除非你禁用了自动检测(不推荐)。
在 ViewModel 中定义并暴露命令属性
命令必须是 public 的属性(不能是字段),且类型为
ICommand,才能被 Binding 找到: 在 ViewModel 类中声明:private readonly ICommand _saveCommand; 构造函数里初始化:_saveCommand = new RelayCommand(Save, CanSave); 暴露为 public 属性:public ICommand SaveCommand => _saveCommand;
Save()是无参方法;
CanSave()返回 bool,比如检查文本框是否非空
XAML 中绑定命令和参数(可选)
Button 等控件有
Command和
CommandParameter属性,直接绑定即可:
<Button Content="保存"
Command="{Binding SaveCommand}"
CommandParameter="{Binding SelectedItem, ElementName=listBox}" />
如果命令方法需要参数,就在
Execute(object parameter)里接收;
CanExecute(object parameter)同理。Binding 会自动把
CommandParameter的值传进去。
系统命令和自定义 RoutedCommand(进阶场景)
WPF 内置了像
ApplicationCommands.Save、
NavigationCommands.BrowseBack这类路由命令。它们本质是
RoutedCommand,不带逻辑,靠在 UI 树中“冒泡/隧道”找到能处理它的
CommandBinding: 在 Window 或 UserControl 的
CommandBindings集合里添加绑定:
<commandbinding command="ApplicationCommands.Save" executed="OnSaveExecuted" canexecute="OnSaveCanExecute"></commandbinding>对应后台方法里写业务逻辑和启用判断 XAML 中 Button 只写
Command="ApplicationCommands.Save"即可,无需指定 ViewModel
适合全局快捷键(如 Ctrl+S)、菜单与工具栏同步响应同一语义命令的场景。
基本上就这些。ICommand 不复杂但容易忽略细节:属性要 public、要 notify CanExecuteChanged(靠 CommandManager 最省心)、参数传递要匹配。用好 RelayCommand + MVVM,界面交互就变得清晰又可测。
