WPF中的命令系统由几个基本要素构成,它们是:
命令(Command): WPF的命令实际上就是实现了ICommand接口的类,平时使用最多的是RoutedCommand类。 ICommand接口在程序集System.dll中,命令空间为System.Windows.Input; 语法[TypeConverterAttribute("System.Windows.Input.CommandConverter,PresentationFramework, Version=4.0.0.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35, Custom=null")]
public interface ICommand
方法
名称 | 说明 |
定义用于确定此命令是否可以在其当前状态下执行的方法。 | |
定义在调用此命令时调用的方法。 |
事件
名称 | 说明 |
当出现影响是否应执行该命令的更改时发生。 |
命令源(CommandSource):
即命令的发送者,是实现了ICommandSource接口的类。
ICommandSource 接口在程序集PresentationCore.dll中,命令空间为:System.Window.Input;
语法
public interface ICommandSource
属性
名称 | 说明 |
获取将在调用命令源时执行的命令。 | |
表示可在执行命令时传递给该命令的用户定义的数据值。 | |
将在其上执行命令的对象。 |
命令目标(Command Target): 即命令将发送给谁,或者说命令将作用在谁身上。命令目标必须是实现IInputElement接口的类。 IInputElement 接口在程序集PresentationCore.dll中,命令空间为:System.Windows; 作用 建立公用事件和事件相关的属性和方法处理的Windows Presentation Foundation(WPF)元素的基本输入的。 命令关联(Command Binding): 负责把一些外围逻辑与命令关联起来,比如执行之前对命令是否可以执行进行判断、命令执行之后还有哪些后续工作等。WPF需要CommandBinding在执行前来帮助判断是不是可以执行、在执行后做一些事件来“打扫战场”。 CommandBinding类在程序集PresentationCore.dll中,命令空间是System.Windows.Input; 作用 将RoutedCommand绑定到实现该命令的事件处理程序。 构造
名称 | 说明 |
CommandBinding() | 初始化 CommandBinding 类的新实例。 |
使用指定的 ICommand 初始化 CommandBinding 类的新实例。 | |
使用指定的 ICommand 和指定的 Executed 事件处理程序初始化 CommandBinding 类的新实例。 | |
CommandBinding(ICommand, ExecutedRoutedEventHandler, CanExecuteRoutedEventHandler) | 使用指定的 ICommand 和指定的 Executed 及 CanExecute 事件处理程序初始化 CommandBinding 类的新实例。 |
属性
名称 | 说明 |
获取或设置与此 CommandBinding 关联的 ICommand。 |
事件
名称 | 说明 |
当与该 CommandBinding 关联的命令启动检查以确定是否可以在命令目标上执行此命令时发生。 | |
当执行与该 CommandBinding 关联的命令时发生。 | |
当与该 CommandBinding 关联的命令启动检查以确定是否可以在当前命令目标上执行此命令时发生。 | |
当执行与该 CommandBinding 关联的命令时发生。 |
以下是一个使用RoutedCommand命令的实例:
MainWindow.xmal
<Window x:Class="CommandFirstTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Command" Height="175" Width="260" Background="LightBlue">
<StackPanel x:Name="stackPanel">
<Button x:Name="button1" Content="Send Command" Margin="5"/>
<TextBox x:Name="textBoxA" Margin="5,0" Height="100"/>
</StackPanel>
</Window>
MainWindow.xmal.cs
using System;
using System.Windows;
using System.Windows.Input;
namespace CommandFirstTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//把命令赋值给命令源(发送者),并指定快捷键
this.button1.Command = clearCmd;
this.clearCmd.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt));
//指令命令目标
this.button1.CommandTarget = this.textBoxA;
//创建命令关联
CommandBinding cb = new CommandBinding();
cb.Command = this.clearCmd; //只关注与clearCmd命令相关的事件
cb.CanExecute += cb_CanExecute;
cb.Executed += cb_Executed;
//把命令安置在外围控件上
this.stackPanel.CommandBindings.Add(cb);
}
/// <summary>
/// 当命令送达目标后,此方法被调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void cb_Executed(object sender, ExecutedRoutedEventArgs e)
{
this.textBoxA.Clear();
//避免继续向上传而影响程序性能
e.Handled = true;
}
/// <summary>
/// 探测命令是否可执行时,此方法被调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (string.IsNullOrEmpty(this.textBoxA.Text))
e.CanExecute = false;
else
e.CanExecute = true;
//避免继续向上传而影响程序性能
e.Handled = true;
Console.WriteLine("这个事件会一直被监听");
}
/// <summary>
/// 声明并自定义命令
/// </summary>
private RoutedCommand clearCmd = new RoutedCommand("Clear", typeof (MainWindow));
}
}
示例来自:深入浅出WPF (176页 - 179页) 9.1 命令系统的基本元素与关系
参考:MSDN