1.1 WPF C# 命令的本质
“粘贴”操作就是命令。
RoutedCommand 类,这是WPF C# 命令的本质。
1.2 WPF C# 命令的机制
1.2.1 编程范围
CanExecuteChanged)。
Execute 执行与命令关联的操作。
CanExecute 确定是否可以在当前命令目标上执行命令。
CanExecuteChanged。
1.2.2 输入源
RoutedCommand 没有不同。
1.2.3 应用程序逻辑
路由事件概述。)
1.2.4 命令引发路由事件
Executed 事件。
PreviewCanExecute 事件。
CommandBinding 的对象。
1.3、WPF C# 命令分类
WPF 通过命令类库提供一组预定义命令。如果命令库类中的命令不满足您的需要,则您可以创建自己的命令。
命令库
ComponentCommands。
Pause 等命令。
实现逻辑由在其上执行命令的对象负责。
例如,如果您指定应用程序处理复制命令,则会自动获得键盘绑定“Ctrl+C”。您还会获得其他输入设备(如 Tablet PC 钢笔笔势和语音信息)的绑定。
ICommand 的类型转换器行为)。
1.3.2 创建自定义命令
如果命令库类中的命令不满足您的需要,则您可以创建自己的命令。
RoutedUICommand。
Create a Custom RoutedCommand Sample(创建自定义 RoutedCommand 示例)。
1.4、WPF C# 命令的用途
命令有若干用途。
1.4.1 将语义以及调用命令的对象与执行命令的逻辑分离
通过使用命令,您可以将各种类型的用户操作绑定到同一逻辑。
1.4.2 操作是否可用
true,则可以启用按钮。
1.4.3 根据目标的类型采取相应的操作
Executed 事件时,它将能访问命令的目标,并根据目标的类型采取相应的操作。
WPF 命令
WPF 命令中的四个主要概念
WPF 中的路由命令模型可以分为四个主要概念:命令、命令源、命令目标以及命令绑定:
-
命令:是要执行的操作。
-
命令源:是调用命令的对象。
-
命令目标:是在其上执行命令的对象。
-
命令绑定:是将命令逻辑映射到命令的对象。
TextBox 控件提供。
CommandBinding 可能附加到命令目标的上级。
XAML
<StackPanel>
<Menu>
<MenuItem Command="ApplicationCommands.Paste" />
</Menu>
<TextBox />
</StackPanel>
C# // Creating the UI objects StackPanel mainStackPanel = new StackPanel(); TextBox pasteTextBox = new TextBox(); Menu stackPanelMenu = new Menu(); MenuItem pasteMenuItem = new MenuItem(); // Adding objects to the panel and the menu stackPanelMenu.Items.Add(pasteMenuItem); mainStackPanel.Children.Add(stackPanelMenu); mainStackPanel.Children.Add(pasteTextBox); // Setting the command to the Paste command pasteMenuItem.Command = ApplicationCommands.Paste; // Setting the command target to the TextBox pasteMenuItem.CommandTarget = pasteTextBox;
2.3 编程设计
2.3.1 命令
CanExecuteChanged。
CommandBinding 的对象。
2.3.2 命令源
KeyGesture 就是命令源。
ICommandSource 接口。
CommandParameter:
-
Command 是在调用命令源时执行的命令。
-
CommandTarget,则具有键盘焦点的元素将是命令目标。
-
CommandParameter 是用户定义的数据类型,用于将信息传递到实现命令的处理程序。
InputBinding。
InputGesture 执行时调用命令。
2.3.2.1 举例: ContextMenu 中的 MenuItem 用作 Properties 命令的命令源
Properties 命令的命令源。
XAML
<StackPanel>
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Command="ApplicationCommands.Properties" />
</ContextMenu>
</StackPanel.ContextMenu>
</StackPanel>
C# StackPanel cmdSourcePanel = new StackPanel(); ContextMenu cmdSourceContextMenu = new ContextMenu(); MenuItem cmdSourceMenuItem = new MenuItem(); // Add ContextMenu to the StackPanel. cmdSourcePanel.ContextMenu = cmdSourceContextMenu; cmdSourcePanel.ContextMenu.Items.Add(cmdSourceMenuItem); // Associate Command with MenuItem. cmdSourceMenuItem.Command = ApplicationCommands.Properties;
MenuItem 会将自己显示为灰色。
2.3.2.2 举例: InputGesture 作命令源
ModifierKeys 组成。
InputBinding。
KeyBinding。
XAML
<Window.InputBindings>
<KeyBinding Key="B"
Modifiers="Control"
Command="ApplicationCommands.Open" />
</Window.InputBindings>
C#
KeyGesture OpenKeyGesture = new KeyGesture(
Key.B,
ModifierKeys.Control);
KeyBinding OpenCmdKeybinding = new KeyBinding(
ApplicationCommands.Open,
OpenKeyGesture);
this.InputBindings.Add(OpenCmdKeybinding);
2.3.2.3 InputGesture 与 RoutedCommand 关联
InputGestureCollection。
InputGestureCollection。
WPF C# 命令的运行机制
命令源
KeyGesture 就是命令源。
ICommandSource 接口。
CommandParameter:
-
Command 是在调用命令源时执行的命令。
-
CommandTarget,则具有键盘焦点的元素将是命令目标。
-
CommandParameter 是用户定义的数据类型,用于将信息传递到实现命令的处理程序。
InputGesture 执行时调用命令。