Skip to content

Commit f9541bf

Browse files
committed
Completed
1 parent 9476299 commit f9541bf

File tree

6 files changed

+223
-4
lines changed

6 files changed

+223
-4
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@
503503
<Compile Include="SamplePages\FocusBehavior\FocusBehaviorPage.xaml.cs">
504504
<DependentUpon>FocusBehaviorPage.xaml</DependentUpon>
505505
</Compile>
506+
<Compile Include="SamplePages\KeyDownTriggerBehavior\KeyDownTriggerBehaviorPage.xaml.cs">
507+
<DependentUpon>KeyDownTriggerBehaviorPage.xaml</DependentUpon>
508+
</Compile>
506509
<Compile Include="SamplePages\MetadataControl\MetadataControlPage.xaml.cs">
507510
<DependentUpon>MetadataControlPage.xaml</DependentUpon>
508511
</Compile>
@@ -626,6 +629,7 @@
626629
<Content Include="SamplePages\Primitives\ConstrainedBox.bind">
627630
<SubType>Designer</SubType>
628631
</Content>
632+
<Content Include="SamplePages\KeyDownTriggerBehavior\KeyDownTriggerBehaviorXaml.bind" />
629633
</ItemGroup>
630634
<ItemGroup>
631635
<Compile Include="App.xaml.cs">
@@ -974,6 +978,10 @@
974978
<Content Include="SamplePages\MetadataControl\MetadataControlCode.bind">
975979
<SubType>Designer</SubType>
976980
</Content>
981+
<Page Include="SamplePages\KeyDownTriggerBehavior\KeyDownTriggerBehaviorPage.xaml">
982+
<Generator>MSBuild:Compile</Generator>
983+
<SubType>Designer</SubType>
984+
</Page>
977985
<Page Include="SamplePages\MetadataControl\MetadataControlPage.xaml">
978986
<SubType>Designer</SubType>
979987
<Generator>MSBuild:Compile</Generator>
@@ -1475,6 +1483,7 @@
14751483
<Name>Visual C++ 2015 Runtime for Universal Windows Platform Apps</Name>
14761484
</SDKReference>
14771485
</ItemGroup>
1486+
<ItemGroup />
14781487
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
14791488
<VisualStudioVersion>14.0</VisualStudioVersion>
14801489
</PropertyGroup>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<Page x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.KeyDownTriggerBehaviorPage"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
xmlns:animations="using:Microsoft.Toolkit.Uwp.UI.Animations"
9+
mc:Ignorable="d">
10+
11+
<StackPanel
12+
VerticalAlignment="Center">
13+
14+
<TextBox
15+
PlaceholderText="Set the focus to this TextBox and press enter to trigger the animation"
16+
Width="300">
17+
<interactivity:Interaction.Behaviors>
18+
<behaviors:KeyDownTriggerBehavior
19+
Key="Enter">
20+
<behaviors:StartAnimationAction Animation="{Binding ElementName=MoveAnimation}" />
21+
</behaviors:KeyDownTriggerBehavior>
22+
</interactivity:Interaction.Behaviors>
23+
</TextBox>
24+
25+
<Button Background="Gray" Margin="0,40,0,0" Width="200" Height="200" HorizontalAlignment="Center" VerticalAlignment="Center">
26+
<animations:Explicit.Animations>
27+
<animations:AnimationSet x:Name="MoveAnimation" IsSequential="True">
28+
<animations:TranslationAnimation Duration="0:0:3" To="0,32,0" From="0,0,0" />
29+
<animations:StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeOutAnimation}" />
30+
<animations:StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeInAnimation}" />
31+
<animations:TranslationAnimation Duration="0:0:1" To="0,0,0" From="0,32,0" />
32+
</animations:AnimationSet>
33+
</animations:Explicit.Animations>
34+
35+
<Image Source="ms-appx:///Assets/ToolkitLogo.png" Height="100" Width="100">
36+
<animations:Explicit.Animations>
37+
<animations:AnimationSet x:Name="FadeOutAnimation">
38+
<animations:OpacityAnimation From="1"
39+
To="0"
40+
Duration="0:0:1"
41+
Delay="0"
42+
EasingType="Linear"
43+
EasingMode="EaseOut" />
44+
</animations:AnimationSet>
45+
<animations:AnimationSet x:Name="FadeInAnimation">
46+
<animations:OpacityAnimation From="0"
47+
To="1"
48+
Duration="0:0:1"
49+
Delay="0"
50+
EasingType="Linear"
51+
EasingMode="EaseOut" />
52+
</animations:AnimationSet>
53+
</animations:Explicit.Animations>
54+
</Image>
55+
56+
</Button>
57+
58+
</StackPanel>
59+
60+
</Page>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Windows.UI.Xaml.Controls;
6+
7+
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
8+
{
9+
/// <summary>
10+
/// A page that shows how to use the AutoFocusBehavior
11+
/// </summary>
12+
public sealed partial class KeyDownTriggerBehaviorPage : Page
13+
{
14+
public KeyDownTriggerBehaviorPage() => InitializeComponent();
15+
}
16+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
mc:Ignorable="d">
8+
9+
<StackPanel
10+
VerticalAlignment="Center">
11+
12+
<TextBox
13+
PlaceholderText="Set the focus to this TextBox and press enter to trigger the animation"
14+
Width="300">
15+
<interactivity:Interaction.Behaviors>
16+
<behaviors:KeyDownTriggerBehavior
17+
Key="Enter">
18+
<behaviors:StartAnimationAction Animation="{Binding ElementName=MoveAnimation}" />
19+
</behaviors:KeyDownTriggerBehavior>
20+
</interactivity:Interaction.Behaviors>
21+
</TextBox>
22+
23+
<Button Background="Gray" Margin="0,40,0,0" Width="200" Height="200" HorizontalAlignment="Center" VerticalAlignment="Center">
24+
<Explicit.Animations>
25+
<AnimationSet x:Name="MoveAnimation" IsSequential="True">
26+
<TranslationAnimation Duration="0:0:3" To="0,32,0" From="0,0,0" />
27+
<StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeOutAnimation}" />
28+
<StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeInAnimation}" />
29+
<TranslationAnimation Duration="0:0:1" To="0,0,0" From="0,32,0" />
30+
</AnimationSet>
31+
</Explicit.Animations>
32+
33+
<Image Source="ms-appx:///Assets/ToolkitLogo.png" Height="100" Width="100">
34+
<Explicit.Animations>
35+
<AnimationSet x:Name="FadeOutAnimation">
36+
<OpacityAnimation From="1"
37+
To="0"
38+
Duration="0:0:1"
39+
Delay="0"
40+
EasingType="Linear"
41+
EasingMode="EaseOut" />
42+
</AnimationSet>
43+
<AnimationSet x:Name="FadeInAnimation">
44+
<OpacityAnimation From="0"
45+
To="1"
46+
Duration="0:0:1"
47+
Delay="0"
48+
EasingType="Linear"
49+
EasingMode="EaseOut" />
50+
</AnimationSet>
51+
</Explicit.Animations>
52+
</Image>
53+
54+
</Button>
55+
56+
</StackPanel>
57+
58+
</Page>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@
785785
"Type": "ThemeListenerPage",
786786
"Subcategory": "Systems",
787787
"About": "The ThemeListener allows you to keep track of changes to the System Theme.",
788-
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs",
788+
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs",
789789
"Icon": "/Assets/Helpers.png",
790790
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/helpers/ThemeListener.md"
791791
},
@@ -839,12 +839,22 @@
839839
"Icon": "/Assets/Helpers.png",
840840
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/AutoSelectBehavior.md"
841841
},
842+
{
843+
"Name": "KeyDownTriggerBehavior",
844+
"Type": "KeyDownTriggerBehaviorPage",
845+
"Subcategory": "Systems",
846+
"About": "Behavior to listen to a key press on a control and executes actions",
847+
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI.Behaviors/Keyboard/KeyDownTriggerBehavior.cs",
848+
"XamlCodeFile": "KeyDownTriggerBehaviorXaml.bind",
849+
"Icon": "/Assets/Helpers.png",
850+
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/KeyboardBehaviors.md"
851+
},
842852
{
843853
"Name": "Win2d Path Mini Language Parser",
844854
"Type": "CanvasPathGeometryPage",
845855
"Subcategory": "Parser",
846856
"About": "CanvasPathGeometry class allows you to convert Win2d Path Mini Language string to CanvasGeometry, Brushes, CanvasStrokes or CanvasStrokeStyles.",
847-
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.UI.Media/Geometry",
857+
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.UI.Media/Geometry",
848858
"Icon": "/SamplePages/CanvasPathGeometry/CanvasPathGeometry.png",
849859
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/parsers/CanvasPathGeometry.md"
850860
},
@@ -882,15 +892,15 @@
882892
"Name": "Guard APIs",
883893
"Subcategory": "Developer",
884894
"About": "The Guard APIs can be used to validate method arguments in a streamlined manner, which is also faster, less verbose, more expressive and less error prone than manually writing checks and throwing exceptions.",
885-
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Diagnostics",
895+
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Diagnostics",
886896
"Icon": "/Assets/Helpers.png",
887897
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/developer-tools/Guard.md"
888898
},
889899
{
890900
"Name": "High Performance APIs",
891901
"Subcategory": "Developer",
892902
"About": "The High Performance package contains a set of APIs that are heavily focused on optimization. All the new APIs have been carefully crafted to achieve the best possible performance when using them, either through reduced memory allocation, micro-optimizations at the assembly level, or by structuring the APIs in a way that facilitates writing performance oriented code in general.",
893-
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.HighPerformance",
903+
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.HighPerformance",
894904
"Icon": "/Assets/Helpers.png",
895905
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/high-performance/Introduction.md"
896906
},
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.Windows.Input;
2+
using Microsoft.Xaml.Interactivity;
3+
using Windows.System;
4+
using Windows.UI.Xaml;
5+
using Windows.UI.Xaml.Input;
6+
7+
namespace Microsoft.Toolkit.Uwp.UI.Behaviors
8+
{
9+
/// <summary>
10+
/// This behavior listens to a key down event on the associated <see cref="UIElement"/> when it is loaded and executes an action.
11+
/// </summary>
12+
[TypeConstraint(typeof(FrameworkElement))]
13+
public class KeyDownTriggerBehavior : Trigger<UIElement>
14+
{
15+
16+
/// <summary>
17+
/// The DP to store the <see cref="Key"/> property value.
18+
/// </summary>
19+
public static readonly DependencyProperty KeyProperty = DependencyProperty.Register(
20+
"Key",
21+
typeof(VirtualKey),
22+
typeof(KeyDownTriggerBehavior),
23+
new PropertyMetadata(null));
24+
25+
/// <summary>
26+
/// Gets or sets the key to listen when the associated object is loaded.
27+
/// </summary>
28+
public VirtualKey Key
29+
{
30+
get => (VirtualKey)GetValue(KeyProperty);
31+
set => SetValue(KeyProperty, value);
32+
}
33+
34+
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
35+
"Command",
36+
typeof(ICommand),
37+
typeof(KeyDownTriggerBehavior),
38+
new PropertyMetadata(null));
39+
40+
/// <inheritdoc/>
41+
protected override void OnAttached()
42+
{
43+
((FrameworkElement)AssociatedObject).KeyDown += OnAssociatedObjectKeyDown;
44+
}
45+
46+
/// <inheritdoc/>
47+
protected override void OnDetaching()
48+
{
49+
((FrameworkElement)AssociatedObject).KeyDown -= OnAssociatedObjectKeyDown;
50+
}
51+
52+
/// <summary>
53+
/// Invokes the current actions when the <see cref="Key"/> is pressed.
54+
/// </summary>
55+
/// <param name="sender">The source <see cref="UIElement"/> instance.</param>
56+
/// <param name="keyRoutedEventArgs">The arguments for the event (unused).</param>
57+
private void OnAssociatedObjectKeyDown(object sender, KeyRoutedEventArgs keyRoutedEventArgs)
58+
{
59+
if (keyRoutedEventArgs.Key == Key)
60+
{
61+
keyRoutedEventArgs.Handled = true;
62+
Interaction.ExecuteActions(sender, Actions, keyRoutedEventArgs);
63+
}
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)