在我们日常的应用程序操作中,经常要处理各种各样的命令和进行相关的事件处理,比如需要复制、粘贴文本框中的内容;上网查看网页时,可能需要返回上一网页查看相应内容;而当我们播放视频和多媒体时,我们可能要调节音量,快速拖动到我们想看的片段等等。在Winform编程中,我们经常使用各种各样的控件来解决此类问题,当然我们也必须编写一堆代码来处理各种各样的命令和事件处理。那么,Windows Presentation Foundation (WPF)作为微软新一代图形图像支援系统,它是如何处理这些命令及事件的呢?

在WPF中,许多控件都自动集成了固有的命令集。比如文本框TextBox就提供了复制(Copy),粘贴(Paste),裁切(Cut),撤消(Undo)和重做(Redo)命令等。

WPF提供常用应用程序所用的命令集,常用的命令集包括:ApplicationCommands, ComponentCommands, NavigationCommands, MediaCommands和EditingCommands

ApplicationCommands(应用程序命令):
 CancelPrint:取消打印
 Close:关闭
 ContextMenu:上下文菜单
 Copy:复制
 CorrectionList: Gets the value that represents the Correction List command.  
 Cut:剪切
 Delete:删除
 Find:查找
 Help:帮助
 New:新建
 NotACommand:不是命令,被忽略
 Open:打开
 Paste:粘贴
 Print:打印
 PrintPreview:打印预览
 Properties:属性
 Redo:重做
 Replace:取代
 Save:保存
 SaveAs:另存为
 SelectAll:选择所有的
 Stop:停止
 Undo:撤消

ComponentCommands(组件命令):
 ExtendSelection:后接Down/Left/Right/Up, 比如:ExtendSelectionDown(Shift+Down,Extend Selection Down),ExtendSelectionLeft等
 Move:后接Down/Left/Right/Up, 如:MoveDown
 MoveFocus:后接Down/Forward/Back/Up, 如:MoveFocusDown
 MoveFocusPage:后接Down/Up,如:MoveFocusPageUp
 MoveTo:后接End/Home/PageDown/PageUp,比如:MoveToPageDown
 ScrollByLine
 ScrollPage:后接Down/Left/Right/Up,比如:ScrollPageLeft
 SelectTo:End/Home/PageDown/PageUp,比如:SelectToEnd

NavigationCommands(导航命令):
 Browse浏览: 后接Back/Forward/Home/Stop, 比如:BrowseBack
 缩放显示:DecreaseZoom, IncreaseZoom, Zoom
 Favorites(收藏)
 页面:FirstPage, LastPage, PreviousPage, NextPage,GoToPage
 NavigateJournal
 Refresh(刷新)
 Search(搜索)

MediaCommands(多媒体控制命令):
 Treble高音:DecreaseTreble,IncreaseTreble
 Bass低音:BoostBass,DecreaseBass,IncreaseBass
 Channel频道:ChannelDown,ChannelUp
 MicrophoneVolume麦克风音量调节:DecreaseMicrophoneVolume,IncreaseMicrophoneVolume,MuteMicrophoneVolume
 ToggleMicrophoneOnOff:麦克风开关
 Volume音量: DecreaseVolume,IncreaseVolume,MuteVolume
 Rewind, FastForward(回放,快进)
 Track轨道:PreviousTrack,NextTrack [上一段(节)]
 Play,Pause,Stop,Record(播放,暂停,停止,录制)
 TogglePlayPause
 Select选择

EditingCommands(编辑/排版类命令):
 Align对齐:AlignCenter,AlignJustify,AlignLeft,AlignRight(居中,撑满,左对齐,右对齐)
 Backspace退格
 TabForward,TabBackward(Tab前缩,Tab向后)
 FontSize字体大小:DecreaseFontSize,IncreaseFontSize
 Indentation缩排:DecreaseIndentation, IncreaseIndentation
 Delete删除: Delete选中部分,DeleteNextWord:删除后一字,DeletePreviousWord:删除前一字
 EnterLineBreak:换行
 EnterParagraphBreak:换段
 CorrectSpellingError/IgnoreSpellingError:纠正/忽略拼写错误
 MoveUpByLine,MoveDownByLine: 上/下移一行,
 MoveUpByPage,MoveDownByPage: 上/下移一页
 MoveUpByParagraph,MoveDownByParagraph: 上/下移一段
 MoveLeftByCharacter/MoveRightByCharacter:左/右移一字符
 MoveLeftByWord/MoveRightByWord 左/右移一词
 MoveToDocumentStart/MoveToDocumentEnd:到文章开头/结尾
 MoveToLineStart/MoveToLineEnd:到一行的开头/结尾
 SelectUpByLine,SelectDownByLine:向上/下选一行
 SelectUpByPage,SelectDownByPage:向上/下选一页
 SelectUpByParagraph,SelectDownByParagraph:向上/下选一段
 SelectLeftByCharacter,SelectRightByCharacter:向左/右选中一字
 SelectLeftByWord,SelectRightByWord:向左/右选中一词
 SelectToDocumentStart,SelectToDocumentEnd: 选中到篇头/篇尾
 SelectToLineStart/SelectToLineEnd:选中到行首/行尾
 ToggleBold, ToggleItalic, ToggleUnderline(加粗,斜体,下划线)
 ToggleBullets, ToggleNumbering(列表:加点,加数字)
 ToggleInsert:插入
 ToggleSuperscript,ToggleSubscript(上标字,下标字)

先来举一个简单的例子:

XAML代码:
<StackPanel>
  <Menu>
    <MenuItem Command="ApplicationCommands.Paste" />
  </Menu>
  <TextBox />
</StackPanel>

C#代码:
StackPanel mainStackPanel = new StackPanel();
TextBox pasteTextBox = new TextBox();
Menu stackPanelMenu = new Menu();
MenuItem pasteMenuItem = new MenuItem();

stackPanelMenu.Items.Add(pasteMenuItem);
mainStackPanel.Children.Add(stackPanelMenu);
mainStackPanel.Children.Add(pasteTextBox);

pasteMenuItem.Command = ApplicationCommands.Paste;

上面代码演示了将对文本框设置为焦点时,菜单项可用,点击菜单项时,将执行粘贴命令。


下面列出关于Command的四个概念和四个小问题:
1、WPF中Command(命令)的四个概念:
(1)命令command:要执行的动作。
(2)命令源command source:发出命令的对象(继承自ICommandSource)。
(3)命令目标command target:执行命令的主体
(4)命令绑定command binding:映射命令逻辑的对象
比如在上面示例中,粘贴(Paste)就是命令(command), 菜单项(MenuItem)是命令源(command source), 文本框(TextBox)是命令目标对象(command target), 命令绑定到command binding文本框(TextBox)控件上。

提示:WPF中的命令都继承自ICommand接口。ICommand暴露两个方法:Execute方法、 CanExecute方法和一个事件:CanExecuteChanged。
继承自ICommandSource的有:ButtonBase, MenuItem, Hyperlink和InputBinding。
而Button,GridViewColumnHeader,ToggleButton,RepeatButton继承自ButtonBase。System.Windows.Input.KeyBinding和MouseBinding继承自InputBinding。

2、四个小问题:
(1)如何指定Command Sources?

XAML:(请将“ApplicationCommands.Properties”换成对应的ApplicationCommands属性值,比如:ApplicationCommands.Copy)
<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();

cmdSourcePanel.ContextMenu = cmdSourceContextMenu;
cmdSourcePanel.ContextMenu.Items.Add(cmdSourceMenuItem);

cmdSourceMenuItem.Command = ApplicationCommands.Properties;

(2)如何指定快捷键?

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);
//也可以这样(下面一句与上面两句的效果等同):
//ApplicationCommands.Open.InputGestures.Add(OpenKeyGesture);
 
(3)如何Command Binding?
XAML代码:
<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/
>
</Window.CommandBindings>

C#代码:
CommandBinding OpenCmdBinding = new CommandBinding(
    ApplicationCommands.Open,
    OpenCmdExecuted,
    OpenCmdCanExecute);

this.CommandBindings.Add(OpenCmdBinding);

具体的事件处理:
C#代码:
void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("The command has been invoked.");
}

void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}

(4)如何设置Command Target并进行绑定Command Binding?
XAML代码:

<StackPanel>
  <Menu>
    <MenuItem Command="ApplicationCommands.Paste"
              CommandTarget="{Binding ElementName=mainTextBox}" />
  </Menu>
  <TextBox Name="mainTextBox"/>
</StackPanel>
 
C#代码:
StackPanel mainStackPanel = new StackPanel();
TextBox mainTextBox= new TextBox();
Menu stackPanelMenu = new Menu();
MenuItem pasteMenuItem = new MenuItem();

stackPanelMenu.Items.Add(pasteMenuItem);
mainStackPanel.Children.Add(stackPanelMenu);
mainStackPanel.Children.Add(mainTextBox);

pasteMenuItem.Command = ApplicationCommands.Paste;

以上例子全是单条命令绑定的情形,事实上,你也可以多个按钮多条命令绑定到同一控件上,比如:
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Orientation="Horizontal" Height="25">
<Button Command="Cut" CommandTarget="{Binding ElementName=textBoxInput}" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/>
<Button Command="Copy" CommandTarget="{Binding ElementName=textBoxInput}" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/>
<Button Command="Paste" CommandTarget="{Binding ElementName=textBoxInput}" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/>
<Button Command="Undo" CommandTarget="{Binding ElementName=textBoxInput}" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/>
<Button Command="Redo" CommandTarget="{Binding ElementName=textBoxInput}" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/>
<TextBox x:Name="textBoxInput" Width="200"/>
</StackPanel>

最后,贴出一个完整点的例子:

XAML代码:
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WPFCommand.Window1"
    Title="MenuItemCommandTask"
 x:Name="Window"
 Width="500"
 Height="400"
    >
<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/>
<CommandBinding Command="Help" CanExecute="HelpCanExecute" Executed="HelpExecuted" />
</Window.CommandBindings>
<Window.InputBindings>
  <KeyBinding Command="HelpKey="F2" />
  <KeyBinding Command="NotACommand" Key="F1" />
</Window.InputBindings>
    <Canvas>
      <Menu DockPanel.Dock="Top">
        <MenuItem Command="ApplicationCommands.Paste" Width="75" />
      </Menu>
      <TextBox BorderBrush="Black" BorderThickness="2" TextWrapping="Wrap" Text="这个TextBox未成为焦点之前,粘贴菜单不可用。" Width="476" Height="41" Canvas.Left="8" Canvas.Top="25"/>
   <Button Command="ApplicationCommands.Open" Height="32" Width="223" Content="测试弹出对话框" Canvas.Left="8" Canvas.Top="70"/>
    </Canvas>
</Window>

对应的C#代码:
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace WPFCommand
{
 public partial class Window1
 {
  public Window1()
  {
   this.InitializeComponent();
  }
        void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("测试弹出对话框,命令已执行!");
        }

void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

void HelpCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.BrawDraw.com/");
        }
 }
}

你不妨试试在程序执行之后,按下F1或F2试试效果,是不是按F2时浏览器指向"http://www.BrawDraw.com/",而按F1时没有任何效果?这是因为这两句:
  <KeyBinding Command="Help" Key="F2" />
  <KeyBinding Command="NotACommand" Key="F1" />
当按F2时,Help命令执行;当按F1时,由于Command="NotACommand",即窗口忽略此命令的执行。

执行效果图:

转载地址:http://blog.csdn.net/johnsuna/article/details/1770602

[转]WPF命令集 Command的更多相关文章

  1. WPF命令(Command)介绍、命令和数据绑定集成应用

    要开始使用命令,必须做三件事: 一:定义一个命令 二:定义命令的实现 三:为命令创建一个触发器 WPF中命令系统的基础是一个相对简单的ICommand的接口,代码如下: public interfac ...

  2. WPF自学入门(十一)WPF MVVM模式Command命令

    在WPF自学入门(十)WPF MVVM简单介绍中的示例似乎运行起来没有什么问题,也可以进行更新.但是这并不是我们使用MVVM的正确方式.正如上一篇文章中在开始说的,MVVM的目的是为了最大限度地降低了 ...

  3. WPF自学入门(十一)WPF MVVM模式Command命令 WPF自学入门(十)WPF MVVM简单介绍

    WPF自学入门(十一)WPF MVVM模式Command命令   在WPF自学入门(十)WPF MVVM简单介绍中的示例似乎运行起来没有什么问题,也可以进行更新.但是这并不是我们使用MVVM的正确方式 ...

  4. 《Ansible权威指南》笔记(3)——Ad-Hoc命令集,常用模块

    五.Ad-Hoc命令集1.Ad-Hoc命令集通过/usr/bin/ansible命令实现:ansible <host-pattern> [options]    -v,--verbose  ...

  5. shell编程:定义简单标准命令集

    shell是用户操作接口的意思,操作系统运行起来后都会给用户提供一个操作界面,这个界面就叫shell,用户可以通过shell来调用操作系统内部的复杂实现,而shell编程就是在shell层次上进行编程 ...

  6. WPF快速入门系列(5)——深入解析WPF命令

    一.引言 WPF命令相对来说是一个崭新的概念,因为命令对于之前的WinForm根本没有实现这个概念,但是这并不影响我们学习WPF命令,因为设计模式中有命令模式,关于命令模式可以参考我设计模式的博文:h ...

  7. 八,WPF 命令

    WPF命令模型 ICommand接口 WPF命令模型的核心是System.Windows.Input.ICommand接口,该接口定义了命令的工作原理,它包含了两个方法和一个事件: public in ...

  8. WPF命令

    WPF的命令是经常使用的,在MVVM中,RelayCommand更是用得非常多,但是命令的本质究竟是什么,有了事件为什么还要命令,命令与事件的区别是什么呢?MVVM里面是如何包装命令的呢?命令为什么能 ...

  9. linux---Vim命令集

    Vim命令集 命令历史 以:和/开头的命令都有历史纪录,能够首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗体中输入下面命令就可以 vim 直接启动vim vim filena ...

随机推荐

  1. 关于DB9一些信号的缩写

    https://www.cnblogs.com/CCJVL/archive/2010/02/04/1663565.html 场景:PCB板子与PC通过RS232连接,以下信号的方向相对于PCB板子而言 ...

  2. SpringBoot生产/开发/测试多环境的选择

    多环境选择 一般一套程序会被运行在多部不同的环境中,比如开发.测试.生产环境,每个环境的数据库地址,服务器端口这些都不经相同,若因为环境的变动而去改变配置的的参数,明显是不合理且易造成错误的 对于不同 ...

  3. 一张图轻松掌握 Flink on YARN 应用启动全流程(上)

    Flink 支持 Standalone 独立部署和 YARN.Kubernetes.Mesos 等集群部署模式,其中 YARN 集群部署模式在国内的应用越来越广泛.Flink 社区将推出 Flink ...

  4. thinkphp 使用php代码

    Php代码可以和标签在模板文件中混合使用,可以在模板文件里面书写任意的PHP语句代码 ,包括下面两种方式: 直线电机选购 第一种:使用php标签 例如: <php>echo 'Hello, ...

  5. thinkphp DEFINED标签

    DEFINED标签用于判断某个常量是否有定义,用法如下: 大理石平台检验标准 <defined name="NAME"> NAME常量已经定义 </defined ...

  6. 线段树+欧拉函数——cf1114F

    调了半天,写线段树老是写炸 /* 两个操作 1.区间乘法 2.区间乘积询问欧拉函数 欧拉函数计算公式 phi(mul(ai))=mul(ai) * (p1-1)/p1 * (p2-1)/p2 * .. ...

  7. 针对发送网络附件的java方法(使用Apache的jar包调用)

    1.先要在pom.xml文件中引入对应的jar包 <!--添加邮件的网络附件 start--> <dependency> <groupId>org.apache.c ...

  8. Vue+Iview+Node 项目结构和配置

    1.项目调整后的目录 api:数据接口定义 assets:静态文件 components:组件  config:项目相关配置  driective:指令   router:路由   store:状态管 ...

  9. 024_mysql应用

    初级照片位置:https://www.cnblogs.com/a276665092/gallery/image/277598.html 好丑啊 ,这怎么改啊!!!! 高级:https://www.cn ...

  10. Java学习之创建对象内存使用机制

    Java内存空间分两种,一种是栈内存,有多个,一种是堆内存,只有一个,在堆内存中又有一块方法区. 方法区中存储的是:类的信息(类名,类的直接父类,类的访问修饰符),类变量,类方法代码,实例方法代码,常 ...