一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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

     命令簡介

     WPF 中的命令是通過實現(xiàn) ICommand 接口創(chuàng)建的。ICommand 公開兩個方法(ExecuteCanExecute)和一個事件(CanExecuteChanged)。Execute 執(zhí)行與命令關(guān)聯(lián)的操作。CanExecute 確定是否可以在當(dāng)前命令目標(biāo)上執(zhí)行命令。如果集中管理命令操作的命令管理器檢測到命令源中發(fā)生了更改,此更改可能使得已引發(fā)但尚未由命令綁定執(zhí)行的命令無效,則將引發(fā) CanExecuteChanged。ICommand 的 WPF 實現(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 提供了一組常用的路由命令,這組命令分布在幾個類中:MediaCommands、ApplicationCommands、NavigationCommandsComponentCommandsEditingCommands。這些類僅包含 RoutedCommand 對象,而不包含命令的實現(xiàn)邏輯。實現(xiàn)邏輯由其上執(zhí)行命令的對象負(fù)責(zé)。[1]

     自定義命令

     除了上述WPF 自帶的RoutedCommand,還可以使用RoutedUICommand 類創(chuàng)建用戶自定義命令,下面將通過一個實例詳細(xì)講解。首先新建一個WPF 項目,在其中加入一個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>

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 為上面兩個命令綁定快捷鍵,按鍵組合可使用“+”進(jìn)行連接。下面代碼分別通過Modifiers+KeyGesture 兩種方式為定義快捷鍵組合方式。大家可以任選其一進(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ò)大字體尺寸時通過CommandBinding_Increase_CanExecute 判斷當(dāng)前字體是否小于50,否則不會執(zhí)行Executed 命令。若字體尺寸在50以內(nèi)則通過CommandBinding_Increase_Executed 每次增加5??s小尺寸時則不低于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#定義,下面繼續(xù)完成修改字體顏色的快捷鍵命令。新建一個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 中加入命名空間,以便后面調(diào)用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"/>

     當(dāng)用戶點擊“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))
);
}

Color

源代碼下載

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

NET技術(shù)WPF 自定義快捷鍵命令(Command),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国内自拍网红在线综合 | 成年美女网站 | 四虎永久免费在线观看 | 91白浆| 日本久久久久久久中文字幕 | 国产精品免费αv视频 | 久99视频| 91视频观看免费 | 91久久线看在观草草青青 | 一级做a爰片久久毛片人呢 一级做a爰片久久毛片图片 | 最近2018中文字幕免费视频 | 久久久久国产一级毛片高清板 | 中文字幕一区在线观看 | 成人国产精品999视频 | 欧美成人三级大全 | 美女黄网站 | 开心激情综合网 | 在线观看免费福利 | 日本高清午夜色wwwσ | 99精品国产兔费观看66 | 91精品国产91久久久久 | 2020天堂中文字幕一区在线观 | 久草视频中文在线 | 国产成人99久久亚洲综合精品 | 国产香蕉久久精品综合网 | 在线观看成人免费 | 玖玖玖视频在线观看视频6 玖玖免费 | 午夜精品一区二区三区在线观看 | 国产女同一区二区在线 | 国产精品亚洲高清一区二区 | 中文字幕一区二区在线播放 | 狠狠ady精品 | 久久久久亚洲香蕉网 | 日韩理论片在线看免费观看 | 久久久网| 91视频麻豆视频 | 一级特黄国产高清毛片97看片 | 中文字幕精品视频在线 | 97人人模人人揉人人捏 | 中文字幕亚洲图片 | 久久久久久精 |