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

作者: Gnie  來源: 博客園  發布時間: 2010-07-17 15:00  閱讀: 5808 次  推薦: 1   原文鏈接   [收藏]  

     命令簡介

     WPF 中的命令是通過實現 ICommand 接口創建的。ICommand 公開兩個方法(ExecuteCanExecute)和一個事件(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 提供了一組常用的路由命令,這組命令分布在幾個類中:MediaCommandsApplicationCommandsNavigationCommandsComponentCommandsEditingCommands。這些類僅包含 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>

TextBlock

     首先在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+KeyGesture 兩種方式為定義快捷鍵組合方式。大家可以任選其一進行使用,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”改變字體大小。

Increase

     除了在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 中加入命名空間,以便后面調用ChangeFontColor 命令。

xmlns:c="clr-namespace:WpfUserControlTest"

     在中為ChangeFontColor 添加快捷鍵組合。

<KeyBinding Modifiers="Control+Alt" Key="C" Command="c:CustomCommand.ChangeFontColor"/>

     在中添加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))
            );
}

Color

源代碼下載

[1]引自:http://msdn.microsoft.com/zh-cn/library/ms752308.aspx

1
0
 
標簽:WPF
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()