// parameter: // 此命令使用的数据。 如果此命令不需要传递数据,则该对象可以设置为 null。 void Execute(object parameter);

这个接口比较关键的是CanExecute和Execute两个方法成员。前者表示当前命令是否可以执行,如果可以的话,WPF命令系统会自动帮我们去调用Execute方法成员。那么,我们要实现这个接口的话,通常只需要在CanExecute编写一些判断逻辑,在Execute调用一个委托就行了。至于这个委托的的签名和具体的代码内容,则是在实际应用时由开发者去编写不同的业务代码。接下来,我们来实现一个ICommand接口。

二、ICommand的实现

public class RelayCommand : ICommand
    public event EventHandler CanExecuteChanged;
    private Action action;
    public RelayCommand(Action action)
        this.action = action;
    public bool CanExecute(object parameter)
        return true;
    public void Execute(object parameter)
        action?.Invoke();
public class MainViewModel : ObservableObject
    public RelayCommand OpenCommand { get; set; } = new RelayCommand(() =>
        MessageBox.Show("Hello,Command");
<Window x:Class="HelloWorld.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:HelloWorld" 
        xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        mc:Ignorable="d" FontSize="14"
        Title="WPF中文网 - 命令 - www.wpfsoft.com" Height="350" Width="500">
    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>
    <Window.Resources>
    </Window.Resources>
        <Button Width="100" Height="30" Content="打开" Command="{Binding OpenCommand}" />
    </Grid>
</Window>
    private Action action;
    private Action<object> objectAction;
    public RelayCommand(Action action)
        this.action = action;
    public RelayCommand(Action<object> objectAction)
        this.objectAction = objectAction;
    public bool CanExecute(object parameter)
        return true;
    public void Execute(object parameter)
        action?.Invoke();
        objectAction?.Invoke(parameter);
public class MainViewModel : ObservableObject
    public RelayCommand OpenCommand { get; set; } = new RelayCommand(() =>
        MessageBox.Show("Hello,Command");
    public RelayCommand OpenParamCommand { get; set; } = new RelayCommand((param) =>
        MessageBox.Show(param.ToString());
<StackPanel VerticalAlignment="Center">
    <Button Width="100" Height="30" Content="打开" Command="{Binding OpenCommand}" />
    <Button Width="100" Height="30" 
            Content="打开" 
            Command="{Binding OpenParamCommand}" 
            CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}}"/>
</StackPanel>
    public event EventHandler CanExecuteChanged;
    public Action<T> Action { get; }
    public RelayCommand(Action<T> action)
        Action = action;
    public bool CanExecute(object parameter)
        return true;
    public void Execute(object parameter)
        Action?.Invoke((T)parameter);
public RelayCommand<object> OpenTParamCommand { get; set; } = new RelayCommand<object>((t) =>
    MessageBox.Show(t.ToString());

当前课程源码下载:(注明:本站所有源代码请按标题搜索)

文件名:071-《ICommand接口》-源代码
链接:https://pan.baidu.com/s/1yu-q4tUtl0poLVgmcMfgBA
提取码:wpff

若文章对您有帮助,可以激励一下我哦,祝您平安幸福! 微信支付宝