1. 程式人生 > >ApplicationCommands 應用程序常見命令

ApplicationCommands 應用程序常見命令

avi gesture tin arch cnblogs dev 事件 n) bass

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="Help" Key="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 應用程序常見命令