WPF 命令库

简介:

        WPF 提供一组预定义命令。 命令库包括以下类:ApplicationCommandsNavigationCommandsMediaCommandsEditingCommands 和 ComponentCommands。 这些类提供诸如 CutBrowseBackBrowseForwardPlayStop 和 Pause 的命令。

        许多这些命令都包含一组默认输入绑定。 例如,如果指定应用程序处理 "复制" 命令,则会自动获取键盘绑定 "Ctrl + C"。

        使用 XAML 引用各个命令库中的命令时,通常可省略公开静态命令属性的库类的类名。 一般来说,命令名称是明确作为字符串的,且存在所属类型来提供命令的逻辑分组,不过对于消除二义性这并不必要。 例如,可指定 Command="Cut" 等价于Command="ApplicationCommands.Cut"。 

      ApplicationCommands 类中的命令以及其他命令库类中的命令,例如 ComponentCommands 和 NavigationCommands,旨在表示应用程序程序员经常遇到的一组常见命令。 命令仅表示 RoutedCommand 的实例,而不表示命令的实现逻辑。 实现逻辑绑定到带有 CommandBinding的命令。 例如,如果对某个控件执行 Close 命令,则该控件可能不会提供执行 Close 命令的逻辑,因此,应用程序编写器将负责编写确定控件如何处理命令的逻辑。

     大部分只是预定义命令,具体的实现是要自己编写实现。快捷键会有默认的,例如ApplicationCommands.Open默认对应"Ctrl + O"。如果需要自定义命令,参考:自定义命令

 

介绍:

ApplicationCommands提供一组与应用程序相关的标准命令。
MediaCommands提供媒体相关命令的标准集。

NavigationCommands

一组标准的与导航相关的命令,包括浏览器和文档导航。

ComponentCommands

提供一组与组件相关的标准命令,这些命令具有预定义的按键输入笔势和 Text 属性。
EditingCommands提供一组标准的与编辑相关的命令。

 

ApplicationCommands Properties
CancelPrint

获取表示“取消打印”命令的值。

Close

获取表示“关闭”命令的值。

ContextMenu

获取表示上下文菜单命令的值。

Copy

获取表示“复制”命令的值。

CorrectionList

获取表示“更正列表”命令的值。

Cut

获取表示“剪切”命令的值。

Delete

获取表示“删除”命令的值。

Find

获取表示 Find 命令的值。

Help

获取表示 Help 命令的值。

New

获取表示 New 命令的值。

NotACommand

表示始终被忽略的命令。

Open

获取表示“打开”命令的值。

Paste

获取表示“粘贴”命令的值。

Print

获取表示“打印”命令的值。

PrintPreview

获取表示“打印预览”命令的值。

Properties

获取表示“属性”命令的值。

Redo

获取表示“重做”命令的值。

Replace

获取表示“替换”命令的值。

Save

获取表示“保存”命令的值。

SaveAs

获取表示“另存为”命令的值。

SelectAll

获取表示“全选”命令的值。

Stop

获取表示“停止”命令的值。

Undo

获取表示“撤销”命令的值。

 

MediaCommands Properties
BoostBass

获取表示“提升基础”命令的值。

ChannelDown

获取表示 Channel Down 命令的值。

ChannelUp

获取表示“频道递增”命令的值。

DecreaseBass

获取表示“降低低音”命令的值。

DecreaseMicrophoneVolume

获取表示 Decrease Microphone Volume 命令的值。

DecreaseTreble

获取表示“降低高音”命令的值。

DecreaseVolume

获取表示“降低音量”命令的值。

FastForward

获取表示 Fast Forward 命令的值。

IncreaseBass

获取表示“提高低音”命令的值。

IncreaseMicrophoneVolume

获取表示“提高麦克风音量”命令的值。

IncreaseTreble

获取表示“提高高音”命令的值。

IncreaseVolume

获取表示“提高音量”命令的值。

MuteMicrophoneVolume

获取表示“将麦克风静音”命令的值。

MuteVolume

获取表示“静音”命令的值。

NextTrack

获取表示“下一曲目”命令的值。

Pause

获取表示“暂停”命令的值。

Play

获取表示“播放”命令的值。

PreviousTrack

获取表示 Previous Track 命令的值。

Record

获取表示“记录”命令的值。

Rewind

获取表示“后退”命令的值。

Select

获取表示“选择”命令的值。

Stop

获取表示“停止”命令的值。

ToggleMicrophoneOnOff

获取表示“打开/关闭麦克风”命令的值。

TogglePlayPause

获取表示“切换播放暂停”命令的值。

 

NavigationCommands Properties
BrowseBack

获取表示 Browse Back 命令的值。

BrowseForward

获取表示 Browse Forward 命令的值。

BrowseHome

获取表示 Browse Home 命令的值。

BrowseStop

获取表示 Browse Stop 命令的值。

DecreaseZoom

获取表示 Decrease Zoom 命令的值。

Favorites

获取表示 Favorites 命令的值。

FirstPage

获取表示 First Page 命令的值。

GoToPage

获取表示 Go To Page 命令的值。

IncreaseZoom

获取表示 Increase Zoom 命令的值。

LastPage

获取表示 Last Page 命令的值。

NavigateJournal

获取表示 Navigate Journal 命令的值。

NextPage

获取表示 Next Page 命令的值。

PreviousPage

获取表示 Previous Page 命令的值。

Refresh

获取表示 Refresh 命令的值。

Search

获取表示 Search 命令的值。

Zoom

获取表示 Zoom 命令的值。

 

ComponentCommands Properties
ExtendSelectionDown

获取表示“向下扩展选择”命令的值。

ExtendSelectionLeft

获取表示 Extend Selection Left 命令的值。

ExtendSelectionRight

获取表示 Extend Selection Right 命令的值。

ExtendSelectionUp

获取表示 Extend Selection Up 命令的值。

MoveDown

获取表示“下移”命令的值。

MoveFocusBack

获取表示“焦点后移”命令的值。

MoveFocusDown

获取表示“焦点下移”命令的值。

MoveFocusForward

获取表示 Move Focus Forward 命令的值。

MoveFocusPageDown

获取表示“焦点页下移”命令的值。

MoveFocusPageUp

获取表示“焦点页上移”命令的值。

MoveFocusUp

获取表示“焦点上移”命令的值。

MoveLeft

获取表示“左移”命令的值。

MoveRight

获取表示“右移”命令的值。

MoveToEnd

获取表示“移动到末尾”命令的值。

MoveToHome

获取表示“移动到开头”命令的值。

MoveToPageDown

获取表示“移到下一页”命令的值。

MoveToPageUp

获取表示“移动到上一页”命令的值。

MoveUp

获取表示“向上移动”命令的值。

ScrollByLine

获取表示“按行滚动”命令的值。

ScrollPageDown

获取表示 Scroll Page Down 命令的值。

ScrollPageLeft

获取表示“向左滚动一页”命令的值。

ScrollPageRight

获取表示“向右滚动一页”命令的值。

ScrollPageUp

获取表示“向上滚动页面”命令的值。

SelectToEnd

获取表示“选择到末尾”命令的值。

SelectToHome

获取表示“选择到开头”命令的值。

SelectToPageDown

获取表示“选择下一页”命令的值。

SelectToPageUp

获取表示“选择上一页”命令的值。

 

EditingCommands Properties
AlignCenter

表示 AlignCenter 命令,该命令请求将当前段落或选中的段落居中。

AlignJustify

表示 AlignJustify 命令,该命令请求将当前段落或选中的段落两端对齐。

AlignLeft

表示 AlignLeft 命令,该命令请求将所选内容左对齐。

AlignRight

表示 AlignRight 命令,该命令请求将所选内容右对齐。

Backspace

表示 Backspace 命令,该命令请求在当前位置或当前所选内容上输入一个退格。

CorrectSpellingError

表示 CorrectSpellingError 命令,该命令请求更正当前位置任何拼写错误的单词。

DecreaseFontSize

表示 DecreaseFontSize 命令,该命令请求将当前所选内容的字号缩小 1 磅。

DecreaseIndentation

表示 DecreaseIndentation 命令,该命令请求将当前段落的缩进减少一个制表位。

Delete

表示 Delete 命令,该命令请求删除当前所选内容。

DeleteNextWord

表示 DeleteNextWord 命令,该命令请求删除下一单词(相对于当前位置而言)。

DeletePreviousWord

表示 DeletePreviousWord 命令,该命令请求删除上一单词(相对于当前位置而言)。

EnterLineBreak

表示 EnterLineBreak 命令,该命令请求在当前位置或当前所选内容上插入一个换行符。

EnterParagraphBreak

表示 EnterParagraphBreak 命令,该命令请求在当前位置或当前所选内容上插入一个分段符。

IgnoreSpellingError

表示 IgnoreSpellingError 命令,该命令请求忽略当前位置或当前所选内容中任何拼写错误的单词的实例。

IncreaseFontSize

表示 IncreaseFontSize 命令,该命令请求将当前所选内容的字号增加 1 磅。

IncreaseIndentation

表示 IncreaseIndentation 命令,该命令请求将当前段落的缩进增加一个制表位。

MoveDownByLine

表示 MoveDownByLine 命令,该命令请求将插入符号向下移动一行。

MoveDownByPage

表示 MoveDownByPage 命令,该命令请求将插入符号向下移动一页。

MoveDownByParagraph

表示 MoveDownByParagraph 命令,该命令请求将插入符号向下移动一个段落。

MoveLeftByCharacter

表示 MoveLeftByCharacter 命令,该命令请求将插入符号向左移动一个字符。

MoveLeftByWord

表示 MoveLeftByWord 命令,该命令请求将插入符号向左移动一个单词。

MoveRightByCharacter

表示 MoveRightByCharacter 命令,该命令请求将插入符号向右移动一个字符。

MoveRightByWord

表示 MoveRightByWord 命令,该命令请求将插入符号向右移动一个单词。

MoveToDocumentEnd

表示 MoveToDocumentEnd 命令,该命令请求将插入符号移至内容的末尾。

MoveToDocumentStart

表示 MoveToDocumentStart 命令,该命令请求将插入符号移至内容的开头。

MoveToLineEnd

表示 MoveToLineEnd 命令,该命令请求将插入符号移至当前行的末尾。

MoveToLineStart

表示 MoveToLineStart 命令,该命令请求将插入符号移至当前行的开头。

MoveUpByLine

表示 MoveUpByLine 命令,该命令请求将插入符号向上移动一行。

MoveUpByPage

表示 MoveUpByPage 命令,该命令请求将插入符号向上移动一页。

MoveUpByParagraph

表示 MoveUpByParagraph 命令,该命令请求将插入符号向上移动一个段落。

SelectDownByLine

表示 SelectDownByLine 命令,该命令请求将当前选择范围向下扩展一行。

SelectDownByPage

表示 SelectDownByPage 命令,该命令请求将当前选择范围向下扩展一页。

SelectDownByParagraph

表示 SelectDownByParagraph 命令,该命令请求将当前选择范围向下扩展一段。

SelectLeftByCharacter

表示 SelectLeftByCharacter 命令,该命令请求将当前选择范围向左扩展一个字符。

SelectLeftByWord

表示 SelectLeftByWord 命令,该命令请求将当前选择范围向左扩展一个单词。

SelectRightByCharacter

表示 SelectRightByCharacter 命令,该命令请求将当前选择范围向右扩展一个字符。

SelectRightByWord

表示 SelectRightByWord 命令,该命令请求将当前选择范围向右扩展一个单词。

SelectToDocumentEnd

表示 SelectToDocumentEnd 命令,该命令请求将当前选择范围扩展到内容末尾处。

SelectToDocumentStart

表示 SelectToDocumentStart 命令,该命令请求将当前选择范围扩展到内容开头处。

SelectToLineEnd

表示 SelectToLineEnd 命令,该命令请求将当前选择范围扩展到当前行的末尾。

SelectToLineStart

表示 SelectToLineStart 命令,该命令请求将当前选择范围扩展到当前行的开头。

SelectUpByLine

表示 SelectUpByLine 命令,该命令请求将当前选择范围向上扩展一行。

SelectUpByPage

表示 SelectUpByPage 命令,该命令请求将当前选择范围向上扩展一页。

SelectUpByParagraph

表示 SelectUpByParagraph 命令,该命令请求将当前选择范围向上扩展一段。

TabBackward

表示 TabBackward 命令。

TabForward

表示 TabForward 命令。

ToggleBold

表示 ToggleBold 命令,该命令请求对当前所选内容进行 Bold 格式切换。

ToggleBullets

表示 ToggleBullets 命令,该命令请求对当前所选内容进行无序列表(也称为项目符号列表)格式切换。

ToggleInsert

表示 ToggleInsert 命令,该命令用于切换“插入”和“改写”键入模式。

ToggleItalic

表示 ToggleItalic 命令,该命令请求对当前所选内容进行 Italic 格式切换。

ToggleNumbering

表示 ToggleNumbering 命令,该命令请求对当前所选内容进行排序列表(也称为编号列表)格式切换。

ToggleSubscript

表示 ToggleSubscript 命令,该命令请求对当前所选内容进行下标格式切换。

ToggleSuperscript

表示 ToggleSuperscript 命令,该命令请求对当前所选内容进行上标格式切换。

ToggleUnderline

表示 ToggleUnderline 命令,该命令请求对当前所选内容进行 Underline 格式切换。

 

EditingCommands Key and ModifierKeys
编辑命令默认键笔势
AlignCenterCtrl+E
AlignJustifyCtrl+J
AlignLeftCtrl+L
AlignRightCtrl+R
BackspaceBackspace
CorrectSpellingError无默认键笔势
DecreaseFontSizeCtrl+OemOpenBrackets
DecreaseIndentationCtrl+Shift+T
DeleteDelete
DeleteNextWordCtrl+Delete
DeletePreviousWordCtrl+Backspace
EnterLineBreakShift+Enter
EnterParagraphBreakEnter
IgnoreSpellingError无默认键笔势
IncreaseFontSizeCtrl+OemCloseBrackets
IncreaseIndentationCtrl+T
MoveDownByLineDown
MoveDownByPagePageDown
MoveDownByParagraphCtrl+Down
MoveLeftByCharacterLeft
MoveLeftByWordCtrl+Left
MoveRightByCharacterRight
MoveRightByWordCtrl+Right
MoveToDocumentEndCtrl+End
MoveToDocumentStartCtrl+Home
MoveToLineEndEnd
MoveToLineStartHome
MoveUpByLineUp
MoveUpByPagePageUp
MoveUpByParagraphCtrl+Up
SelectDownByLineShift+Down
SelectDownByPageShift+PageDown
SelectDownByParagraphCtrl+Shift+Down
SelectLeftByCharacterShift+Left
SelectLeftByWordCtrl+Shift+Left
SelectRightByCharacterShift+Right
SelectRightByWordCtrl+Shift+Right
SelectToDocumentEndCtrl+Shift+End
SelectToDocumentStartCtrl+Shift+Home
SelectToLineEndShift+End
SelectToLineStartShift+Home
SelectUpByLineShift+Up
SelectUpByPageShift+PageUp
SelectUpByParagraphCtrl+Shift+Up
TabBackwardShift+Tab
TabForwardTab
ToggleBoldCtrl+B
ToggleBulletsCtrl+Shift+L
ToggleInsertInsert
ToggleItalicCtrl+I
ToggleNumberingCtrl+Shift+N
ToggleSubscriptCtrl+OemPlus
ToggleSuperscriptCtrl+Shift+OemPlus
ToggleUnderlineCtrl+U

 

使用:

1.ApplicationCommands

使用ApplicationCommands.Open

    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Open" Executed="command_Execute" CanExecute="command_CanExecute"/>
    </Window.CommandBindings>
    <Grid>
        <StackPanel >
            <Button Height="20"  Command="ApplicationCommands.Open" Content="{Binding RelativeSource={RelativeSource Mode=Self},Path=Command.Text}"></Button>
        </StackPanel>
    </Grid>

 

快捷键设置"Ctrl + O" 

    <Window.InputBindings>
        <KeyBinding Key="O" Modifiers="Ctrl" Command="ApplicationCommands.Open"></KeyBinding>
    </Window.InputBindings>
    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Open" Executed="command_Execute" CanExecute="command_CanExecute"/>
    </Window.CommandBindings>
    <Grid>
        <StackPanel >
            <Button Height="20"  Command="ApplicationCommands.Open" Content="{Binding RelativeSource={RelativeSource Mode=Self},Path=Command.Text}"></Button>
        </StackPanel>
    </Grid>

 

等价替换 

            KeyGesture key = new KeyGesture(Key.O, ModifierKeys.Control);
            KeyBinding bindingkey = new KeyBinding(ApplicationCommands.Open, key);
            this.InputBindings.Add(bindingkey);

            CommandBinding bindingCmd = new CommandBinding(ApplicationCommands.Open, command_Execute, command_CanExecute);
            this.CommandBindings.Add(bindingCmd);

 

命令实现 

        public void command_Execute(object sender, ExecutedRoutedEventArgs e)
        {    
            MessageBox.Show(e.Source.ToString());
            e.Handled = true; //停止路由事件传播
        }

        public void command_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;
        }

 

 ApplicationCommands的命令本质上是RoutedUICommand

 

2.MediaCommands

使用Play命令

    <Window.CommandBindings>
        <CommandBinding Command="MediaCommands.Play" Executed="command_Execute" CanExecute="command_CanExecute"></CommandBinding>
    </Window.CommandBindings>

    <Grid>
        <StackPanel>
            <Button Command="MediaCommands.Play" Content="{Binding RelativeSource={RelativeSource Mode=Self},Path=Command.Text}"  Height="20"></Button>
        </StackPanel>
    </Grid>

 

        public void command_Execute(object sender, ExecutedRoutedEventArgs e)
        {    
            MessageBox.Show(e.Source.ToString());
            e.Handled = true; //停止路由事件传播
        }

        public void command_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;
        }

 

 

3.NavigationCommands

 通过Frame使两个Page切换,将 Refresh命令 与 Frame结合使用。 

    <Window.CommandBindings>
        <CommandBinding Command="NavigationCommands.Refresh" Executed="command_Execute" CanExecute="command_CanExecute"></CommandBinding>
    </Window.CommandBindings>

    <Grid>
        <StackPanel>
            <MenuItem Command="NavigationCommands.Refresh" CommandTarget="{Binding ElementName=frame}"></MenuItem>
            <Frame x:Name="frame" NavigationUIVisibility="Hidden" Source="Page1.xaml" Height="200" ></Frame>
        </StackPanel>
    </Grid>
        public void command_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            this.frame.NavigationUIVisibility = NavigationUIVisibility.Visible;
            this.frame.Source = new Uri("Page2.xaml",UriKind.Relative);
        }

 

 Page添加

 

Page的背景色设置 

    <Grid>
        <Border Background="Blue"></Border>
    </Grid>

 

 

4.ComponentCommands

按钮下移动画

    <Window.CommandBindings>
        <CommandBinding Command="ComponentCommands.MoveDown" Executed="command_Execute" CanExecute="command_CanExecute"></CommandBinding>
    </Window.CommandBindings>

    <Canvas>
       <Button x:Name="btn" Command="ComponentCommands.MoveDown" Content="{Binding RelativeSource={RelativeSource Mode=Self},Path=Command.Text}"  Height="20" Width="50"></Button>
    </Canvas>
        public void command_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation(0, 300, new Duration(TimeSpan.FromSeconds(1)));
            Storyboard.SetTarget(doubleAnimation, this.btn);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(Canvas.TopProperty));
            Storyboard story = new Storyboard();
            story.Completed += Story_Completed;
            story.Children.Add(doubleAnimation);
            story.Begin();       
        }

        private void Story_Completed(object sender, EventArgs e)
        {
            //完成后
        }

 

5.EditingCommands

  1. Execute 方法的第一个参数是命令参数。 大多数编辑命令忽略 command 参数;通常为null。第二个参数指定要将命令路由到的对象。 此对象必须实现 IInputElement 接口,并包括指定命令的处理程序。 
  2. RichTextBox支持此命令。如果在不处理命令的对象上调用命令时,不会执行。

 

如下表示 AlignCenter 命令,该命令请求将当前段落或选中的段落居中。此命令的默认键为 Ctrl+E

    <Grid>
        <StackPanel>
            <RichTextBox x:Name="rtbx"></RichTextBox>
            <Button Content="Align Center" Click="Button_Click"></Button>
        </StackPanel>
    </Grid>
        public void Button_Click(object sender, RoutedEventArgs e)
        {
            EditingCommands.AlignCenter.Execute(null, this.rtbx);//居中
        }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值