WPF命令绑定核心是ICommand接口,通过RelayCommand等实现类封装执行逻辑与可用性判断,ViewModel中以public ICommand属性暴露,XAML中用Command和CommandParameter绑定,配合CommandManager.RequerySuggested自动更新状态。

WPF 中命令绑定的核心是 ICommand 接口,它让 UI 元素(如 Button、MenuItem)能“声明式”地触发逻辑,而不直接耦合事件处理代码。关键不是写一堆 Click 事件,而是把“能执行什么”和“什么时候可执行”封装成对象,再通过 XAML 绑定过去。
微软没提供默认实现,但社区广泛使用 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() ——除非你禁用了自动检测(不推荐)。
命令必须是 public 的属性(不能是字段),且类型为 ICommand,才能被 Binding 找到:
Save() 是无参方法;CanSave() 返回 bool,比如检查文本框是否非空Button 等控件有 Command 和 CommandParameter 属性,直接绑定即可:
<Button Content="保存"
Command="{Binding SaveCommand}"
CommandParameter="{Binding SelectedItem, ElementName=listBox}" />如果命令方法需要参数,就在 Execute(object parameter) 里接收;CanExecute(object parameter) 同理。Binding 会自动把 CommandParameter 的值传进去。
WPF 内置了像 ApplicationCommands.Save、NavigationCommands.BrowseBack 这类路由命令。它们本质是 RoutedCommand,不带逻辑,靠在 UI 树中“冒泡/隧道”找到能处理它的 CommandBinding:
CommandBindings 集合里添加绑定:<commandbinding command="ApplicationCommands.Save" executed="OnSaveExecuted" canexecute="OnSaveCanExecute"></commandbinding>
Command="ApplicationCommands.Save" 即可,无需指定 ViewModel适合全局快捷键(如 Ctrl+S)、菜单与工具栏同步响应同一语义命令的场景。
基本上就这些。ICommand 不复杂但容易忽略细节:属性要 public、要 notify CanExecuteChanged(靠 CommandManager 最省心)、参数传递要匹配。用好 RelayCommand + MVVM,界面交互就变得清晰又可测。
以上就是WPF怎么使用命令绑定 WPF ICommand接口实现方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号