ApplicationCommands用于表示应用程序程序员经常遇到的常见命令,类似于ctrl+c

在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

ApplicationCommands用于表示应用程序程序员经常遇到的常见命令,类似于ctrl+c

原文:http://www.cnblogs.com/chuhaida/p/4166438.html

ApplicationCommands 应用程序常见命令的更多相关文章

  1. 后端程序员必备的 Linux 基础知识+常见命令(近万字总结)

    大家好!我是 Guide 哥,Java 后端开发.一个会一点前端,喜欢烹饪的自由少年. 今天这篇文章中简单介绍一下一个 Java 程序员必知的 Linux 的一些概念以及常见命令. 如果文章有任何需要 ...

  2. windows cmd: 打开windows系统程序或服务的常见命令

    Windows常用CMD命令 http://www.cnblogs.com/sbaicl/archive/2013/03/05/2944001.html 其实查找Windows自带程序的命令行很简单, ...

  3. [Linux基础]Linux基础知识入门及常见命令.

    前言:最近刚安装了Linux系统, 所以学了一些最基本的操作, 在这里把自己总结的笔记记录在这里. 1,V8:192.168.40.10V1:192.168.40.11Linux ip:192.168 ...

  4. 嵌入式Linux开发教程:Linux常见命令(上篇)

    摘要:这是对周立功编著的<嵌入式Linux开发教程>的第7期连载.本期刊载内容有关LinuxLinux常见命令中的导航命令.目录命令和文件命令.下一期将连载网络操作命令.安装卸载文件系统等 ...

  5. shell中bash的常见命令

    shell 在计算机科学中,Shell俗称壳,用来区别Kernel(核) Shell分类:1:图形界面shell:通过提供友好的可视化界面,调用相应应用程序,如windows系列操作系统,Linux系 ...

  6. 官方问答--微信小程序常见FAQ (17.8.21-17.8.27)

    给提问的开发者的建议:提问之前先查询 文档.通过社区右上角搜索搜索已经存在的问题. 写一个简明扼要的标题,并且正文描述清楚你的问题. 提交 BUG:需要带上基础库版本号,设备信息(iOS, Andro ...

  7. mysql 性能优化常见命令

    mysql 性能优化常见命令: 一: 当发现mysql程序运行缓慢时,在排除sql主机问题之后,可以尝试在schema,table,和sql上进一步进行考查: 1:mysql> show ful ...

  8. linux的基本操作与常见命令

    linux的基本操作与常见命令: jdk的安装: 步骤:(特别注意:虚拟机安装的一般是32位的操作系统,jdk也必须使用32位的) 查看虚拟机版本:sudo uname --m i686 //表示是3 ...

  9. 05 Linux系统下的用户以及用户权限管理(权限管理介绍、用户管理、常见命令介绍)

    这一节我们介绍Linux的用户以及权限管理的前半段,包括:1.权限管理介绍: 2.用户管理: 3.常见命令 权限管理介绍 权限管理: 为了访问计算机资源,我们需要对其进行授权才能访问,根据什么东西来进 ...

随机推荐

  1. MyBatis整体了解

    背景资料 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBati ...

  2. mongodb数据库高级操作

    1.创建索引 2.索引名称 3.其他索引 4.explain 5.操作索引 6.高级特性 7.固定集合 8.导入导出 9.上锁 10.添加用户 11.主从复制

  3. 望岳物业APP开发过程

    望岳物业APP开发过程 1.望岳组员们讨论决定了做的项目及模块功能. 2.物业管理APP图标设计以及写项目的ER图,主要功能流程图. 3.项目体系结构设计和界面设计. 4.了解物业APP的几个功能,然 ...

  4. 关于<!DOCTYPE html>的学习(转)

    DOCTYPE是对Document type的缩写,说明用XHTML或者HTML是什么版本的.必须出现在<html>标签的前面,不需要关闭标签. <!DOCTYPE>声明不是标 ...

  5. libevent 多线程

    对于evbuffer,如果libevent使用了evthread_use_pthreads();那么所有的单个evbuffer操作就已经是原子的了,调用操作相关的接口进去就上锁,出来解锁,那么 evb ...

  6. css3 text-fill-color简介

    text-fill-color是什么意思呢?单单从字面上来看就是“文本填充颜色”,不过它实际也是设置对象中文字的填充颜色,和color的效果很相似.如果同时设置text-fill-color和colo ...

  7. Firefox浏览器浏览自己做的网站需要输入用户名和密码解决

    我用最新的Firefox 35.0浏览我制作的网站,就会弹出这个对话框.这是什么原因?

  8. Hello to the cruel world

  9. [SDOI2010]星际竞速——费用流

    类似于最短路的网络流,而且还要保证每个点经过一次,拆点就比较方便了. 连边怎么连?要保证最大流是n(每个点经过一次)还要能从直接跳转 将每个点拆点.源点向每个点的入点连一条容量为1费用为0的边.源点向 ...

  10. Codeforces Round #350 (Div. 2) A

    A. Holidays time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...