程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WPF自定義快捷鍵命令(Command)

WPF自定義快捷鍵命令(Command)

編輯:關於.NET

命令簡介

WPF 中的命令是通過實現 ICommand 接口創建的。ICommand 公開兩個方法(Execute 及 CanExecute)和一個事件(CanExecuteChanged)。 Execute 執行與命令關聯的操作。CanExecute 確定是否可以在當前命令目標上執行命令。如果集中管理命令操作的命令管理器檢測到命令源中發生了更改,此更改可能使得已引發但尚未由命令綁定執行的命令無效,則將引發 CanExecuteChanged。ICommand 的 WPF 實現是 RoutedCommand 類。

WPF 中的主要輸入源是鼠標、鍵盤、墨跡和路由命令。更加面向設備的輸入使用 RoutedEvent 來通知應用程序頁中的對象已發生了輸入事件。RoutedCommand 沒有不同。RoutedCommand 的 Execute 和 CanExecute 方法不包含命令的應用程序邏輯,而是引發這樣的路由事件:沿元素樹以隧道和冒泡形式傳遞,直到遇到具有 CommandBinding 的對象。CommandBinding 包含這些事件的處理程序,執行此命令的就是這些處理程序。

RoutedCommand 上的 Execute 方法在命令目標上引發 PreviewExecuted 和 Executed 事件。RoutedCommand 上的 CanExecute 方法在命令目標上引發 CanExecute 和 PreviewCanExecute 事件。這些事件沿元素樹以隧道和冒泡形式傳遞,直到遇到具有該特定命令的 CommandBinding 的對象。

WPF 提供了一組常用的路由命令,這組命令分布在幾個類中:MediaCommands、ApplicationCommands、NavigationCommands、ComponentCommands 和 EditingCommands。這些類僅包含 RoutedCommand 對象,而不包含命令的實現邏輯。實現邏輯由其上執行命令的對象負責。[1]

自定義命令

除了上述WPF 自帶的RoutedCommand,還可以使用RoutedUICommand 類創建用戶自定義命令,下面將通過一個實例詳細講解。首先新建一個WPF 項目,在其中加入一個TextBlock。目的是通過快捷鍵組合“Ctrl+Alt+I”和“Ctrl+Alt+D”改變字體大小,由“Ctrl+Alt+C”隨機改變字體顏色。

<Grid>
   <TextBlock x:Name="textBlock1" Text="Hello World" HorizontalAlignment="Center"
         FontSize="10" Margin="42,29,46,41" Width="293" />

</Grid>

首先在Window.Resources 中定義兩個RoutedUICommand,分別用於增加和減小字體尺寸。

<Window.Resources>
   <RoutedUICommand x:Key="IncreaseFontSize" Text="Increase Font Size" />
   <RoutedUICommand x:Key="DecreaseFontSize" Text="Decrease Font Size" />
</Window.Resources>

通過KeyBinding 為上面兩個命令綁定快捷鍵,按鍵組合可使用“+”進行連接。下面代碼分別通過Modifiers+Key 和Gesture 兩種方式為定義快捷鍵組合方式。大家可以任選其一進行使用,MSDN 中建議使用Gesture 方式定義以免發生混淆。

<Window.InputBindings>
   <KeyBinding Modifiers="Ctrl+Alt" Key="I" Command="{StaticResource IncreaseFontSize}"/>
   <KeyBinding Gesture="Ctrl+Alt+D" Command="{StaticResource DecreaseFontSize}"/>
</Window.InputBindings>

接下來就要通過CanExecute和Excuted 為命令綁定相關的事件,CanExecute 負責判斷能否執行命令(即Executed 定義的事件),Executed 就負責去執行用戶定義的操作命令。

<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 中對命令的定義已經完成。下面進入到C# 中編寫命令事件相關內容。擴大字體尺寸時通過CommandBinding_Increase_CanExecute 判斷當前字體是否小於50,否則不會執行Executed 命令。若字體尺寸在50以內則通過CommandBinding_Increase_Executed 每次增加5。縮小尺寸時則不低於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;
}

運行程序使用“Ctrl+Alt+I”或 “Ctrl+Alt+D”改變字體大小。

除了在XAML 中定義RoutedUICommand 我們也可以直接用C#定義,下面繼續完成修改字體顏色的快捷鍵命令。新建一個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> 中加入命名空間,以便後面調用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"/>

當用戶點擊“Ctrl+Alt+C”是觸發命令事件,最近改變字體顏色。

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))
       );
}

出處:http://www.cnblogs.com/gnielee/

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved