WPF 自定義快捷鍵命令(Command)命令簡介WPF 中的命令是通過實(shí)現(xiàn) ICommand 接口創(chuàng)建的。ICommand 公開兩個(gè)方法(Execute 及 CanExecute)和一個(gè)事件(CanExecuteChanged)。Execute 執(zhí)行與命令關(guān)聯(lián)的操作。CanExecute 確定是否可以在當(dāng)前命令目標(biāo)上執(zhí)行命令。如果集中管理命令操作的命令管理器檢測到命令源中發(fā)生了更改,此更改可能使得已引發(fā)但尚未由命令綁定執(zhí)行的命令無效,則將引發(fā) CanExecuteChanged。ICommand 的 WPF 實(shí)現(xiàn)是 RoutedCommand 類。 WPF 中的主要輸入源是鼠標(biāo)、鍵盤、墨跡和路由命令。更加面向設(shè)備的輸入使用 RoutedEvent 來通知應(yīng)用程序頁中的對象已發(fā)生了輸入事件。RoutedCommand 沒有不同。RoutedCommand 的 Execute 和 CanExecute 方法不包含命令的應(yīng)用程序邏輯,而是引發(fā)這樣的路由事件:沿元素樹以隧道和冒泡形式傳遞,直到遇到具有 CommandBinding 的對象。CommandBinding 包含這些事件的處理程序,執(zhí)行此命令的就是這些處理程序。 RoutedCommand 上的 Execute 方法在命令目標(biāo)上引發(fā) PreviewExecuted 和 Executed 事件。RoutedCommand 上的 CanExecute 方法在命令目標(biāo)上引發(fā) CanExecute 和 PreviewCanExecute 事件。這些事件沿元素樹以隧道和冒泡形式傳遞,直到遇到具有該特定命令的 CommandBinding 的對象。 WPF 提供了一組常用的路由命令,這組命令分布在幾個(gè)類中:MediaCommands、ApplicationCommands、NavigationCommands、ComponentCommands 和 EditingCommands。這些類僅包含 RoutedCommand 對象,而不包含命令的實(shí)現(xiàn)邏輯。實(shí)現(xiàn)邏輯由其上執(zhí)行命令的對象負(fù)責(zé)。[1] 自定義命令除了上述WPF 自帶的RoutedCommand,還可以使用RoutedUICommand 類創(chuàng)建用戶自定義命令,下面將通過一個(gè)實(shí)例詳細(xì)講解。首先新建一個(gè)WPF 項(xiàng)目,在其中加入一個(gè)TextBlock。目的是通過快捷鍵組合“Ctrl+Alt+I”和“Ctrl+Alt+D”改變字體大小,由“Ctrl+Alt+C”隨機(jī)改變字體顏色。 <Grid> <TextBlock x:Name="textBlock1" Text="Hello World" HorizontalAlignment="Center" FontSize="10" Margin="42,29,46,41" Width="293" /> </Grid> ![]() 首先在Window.Resources 中定義兩個(gè)RoutedUICommand,分別用于增加和減小字體尺寸。 <Window.Resources> <RoutedUICommand x:Key="IncreaseFontSize" Text="Increase Font Size" /> <RoutedUICommand x:Key="DecreaseFontSize" Text="Decrease Font Size" /> </Window.Resources> 通過KeyBinding 為上面兩個(gè)命令綁定快捷鍵,按鍵組合可使用“+”進(jìn)行連接。下面代碼分別通過Modifiers+Key 和Gesture 兩種方式為定義快捷鍵組合方式。大家可以任選其一進(jìn)行使用,MSDN 中建議使用Gesture 方式定義以免發(fā)生混淆。 <Window.InputBindings> <KeyBinding Modifiers="Ctrl+Alt" Key="I" Command="{StaticResource IncreaseFontSize}"/> <KeyBinding Gesture="Ctrl+Alt+D" Command="{StaticResource DecreaseFontSize}"/> </Window.InputBindings> 接下來就要通過CanExecute和Excuted 為命令綁定相關(guān)的事件,CanExecute 負(fù)責(zé)判斷能否執(zhí)行命令(即Executed 定義的事件),Executed 就負(fù)責(zé)去執(zhí)行用戶定義的操作命令。 <Window.CommandBindings> <CommandBinding Command="{StaticResource IncreaseFontSize}" CanExecute="CommandBinding_Increase_CanExecute" Executed="CommandBinding_Increase_Executed"/> <CommandBinding Command="{StaticResource DecreaseFontSize}" CanExecute="CommandBinding_Decrease_CanExecute" Executed="CommandBinding_Decrease_Executed"/> </Window.CommandBindings> 至此,我們在XAML 中對命令的定義已經(jīng)完成。下面進(jìn)入到C# 中編寫命令事件相關(guān)內(nèi)容。擴(kuò)大字體尺寸時(shí)通過CommandBinding_Increase_CanExecute 判斷當(dāng)前字體是否小于50,否則不會執(zhí)行Executed 命令。若字體尺寸在50以內(nèi)則通過CommandBinding_Increase_Executed 每次增加5??s小尺寸時(shí)則不低于5。 private void CommandBinding_Increase_CanExecute(object sender, CanExecuteRoutedEventArgs e) { if (textBlock1.FontSize > 50) { e.CanExecute = false; } else { e.CanExecute = true; } } private void CommandBinding_Increase_Executed(object sender, ExecutedRoutedEventArgs e) { textBlock1.FontSize += 5; } private void CommandBinding_Decrease_CanExecute(object sender, CanExecuteRoutedEventArgs e) { if (textBlock1.FontSize <= 5) { e.CanExecute = false; } else { e.CanExecute = true; } } private void CommandBinding_Decrease_Executed(object sender, ExecutedRoutedEventArgs e) { textBlock1.FontSize -= 5; } 運(yùn)行程序使用“Ctrl+Alt+I”或 “Ctrl+Alt+D”改變字體大小。 除了在XAML 中定義RoutedUICommand 我們也可以直接用C#定義,下面繼續(xù)完成修改字體顏色的快捷鍵命令。新建一個(gè)CustomCommand 類,在其中加入如下代碼定義ChangeFontColor 命令。 using System.Windows.Input; namespace WpfUserControlTest { class CustomCommand { public static readonly RoutedUICommand ChangeFontColor = new RoutedUICommand("Change Font Color", "ChangeFontColor", typeof(MainWindow)); } } 在MainWindow.xaml <Window> 中加入命名空間,以便后面調(diào)用ChangeFontColor 命令。 xmlns:c="clr-namespace:WpfUserControlTest" 在<Window.InputBindings>中為ChangeFontColor 添加快捷鍵組合。 <KeyBinding Modifiers="Control+Alt" Key="C" Command="c:CustomCommand.ChangeFontColor"/> 在<Window.CommandBindings>中添加CanExecute、Excuted 命令事件。 <CommandBinding Command="c:CustomCommand.ChangeFontColor" CanExecute="CommandBinding_Color_CanExecute" Executed="CommandBinding_Color_Executed"/> 當(dāng)用戶點(diǎn)擊“Ctrl+Alt+C”是觸發(fā)命令事件,最近改變字體顏色。 private void CommandBinding_Color_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } private void CommandBinding_Color_Executed(object sender, ExecutedRoutedEventArgs e) { Random rd = new Random(); textBlock1.Foreground = new SolidColorBrush( Color.FromRgb( (byte)rd.Next(0,255), (byte)rd.Next(0, 255), (byte)rd.Next(0, 255)) ); } ![]() 源代碼下載WpfCustomHotkeyCommand.zip |
|