這裡用一個簡單的範例來說明,就是當鍵盤同時按下Ctrl+Alt+A三個按鍵時,背景會改變顏色,作法很簡單首先就是從Blend的工具箱中拖曳ChangeProperty Action到LayoutRoot上
然後設定Triggertype為KeyTrigger,FiredOn為KeyDown表示是鍵盤按下時去觸發,Key屬性設定為A鍵,最後Modifiers屬性因為這裡要設定Alt跟Ctrl兩個按鍵,而在下拉選單中只能選取一個按鍵無法一次選擇兩個,所以這邊就切換到Xaml的檢視畫面手動打入Modifiers="Control,Alt",如此就可以透過Ctrl+Alt+A的複合鍵來達到功能,XAML代碼如下
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Grid x:Name="LayoutRoot" Background="White"> | |
<i:Interaction.Triggers> | |
<ei:KeyTrigger Key="A" Modifiers="Control,Alt" > | |
<ei:ChangePropertyAction PropertyName="Background" TargetName="LayoutRoot"> | |
<ei:ChangePropertyAction.Value> | |
<SolidColorBrush Color="Red"/> | |
</ei:ChangePropertyAction.Value> | |
</ei:ChangePropertyAction> | |
</ei:KeyTrigger> | |
</i:Interaction.Triggers> | |
</Grid> |
而在WPF中,也同樣的可以設定複合鍵功能,做法跟Silverlight完全一樣所以這邊就不再重複,但是唯一的小差異是,在WPF中Modifiers屬性如果要設定複合鍵,要用"+"號連結,也就是Modifiers="Control+Alt",而剛剛的Silverlight則是用","連結,兩者的差異也就只在這裡而已,WPF的XAML代碼如下
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Grid x:Name="LayoutRoot" Background="White"> | |
<i:Interaction.Triggers> | |
<ei:KeyTrigger Key="A" Modifiers="Control+Alt" > | |
<ei:ChangePropertyAction PropertyName="Background" TargetName="LayoutRoot"> | |
<ei:ChangePropertyAction.Value> | |
<SolidColorBrush Color="Red"/> | |
</ei:ChangePropertyAction.Value> | |
</ei:ChangePropertyAction> | |
</ei:KeyTrigger> | |
</i:Interaction.Triggers> | |
</Grid> |
0 意見:
張貼留言