wpf路由——路由命令

WPF 中的路由命令模型可以分为四个主要概念:命令、命令源、命令目标以及命令绑定:

  • 命令是要执行的操作。e.g  ApplicationCommands.Copy、Cut、Paste

  • 命令源是调用命令的对象。 e.g MenuItem、Button

  • 命令目标是在其上执行命令的对象。 e.g 在TextBox上执行Cut

  • 命令绑定是将命令逻辑映射到命令的对象。

一 命令:

 

命令类

示例命令

ApplicationCommands

Close、Cut、Copy、Paste、Save、Print

NavigationCommands

BrowseForward、BrowseBack、Zoom、Search

EditingCommands

AlignXXX、MoveXXX、SelectXXX

MediaCommands

Play、Pause、NextTrack、IncreaseVolume、Record、Stop

ComponentCommands

MoveXXX、SelectXXX、ScrollXXX、ExtendSelectionXXX

 

二、命令源

 

1、直接使用控件的Command属性绑定命令:

e.g: < Button Command ="ApplicationCommands.Copy" />

2、绑定快捷键

 

< KeyBinding Command ="ApplicationCommands.Open" Key ="F2" Modifiers ="Control" />
三、命令目标


即就是命令执行的对象。此时必须用 CommandTarget 属性实现。

e.g :

< MenuItem x:Name ="menuPaste" Header ="Paste" Command ="ApplicationCommands.Paste" CommandTarget ="{Binding ElementName=txtMain}" />

 

四、命令绑定

 

使用<keyBinding>将命令绑定。

e.g :

< Window.InputBindings >

       < KeyBinding Command ="ApplicationCommands.Save" Key ="F3" Modifiers ="Control" />  

</ Window.InputBindings >

< Window.CommandBindings >

       < CommandBinding Command ="ApplicationCommands.Save"

           CanExecute ="CommandBinding_Save_CanExecute"

           Executed ="CommandBinding_Save_Executed" />

</ Window.CommandBindings >

 

 

你可能感兴趣的:(WPF)