ApplicationCommands用於表示應用程式程式員經常遇到的常見命令,類似於ctrl+c

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   os   sp   for   

在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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.