-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4160 from ArchieCoder/feature/KeyDownTriggerBehavior
New behavior KeyDownTriggerBehavior
- Loading branch information
Showing
5 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
59 changes: 59 additions & 0 deletions
59
....Toolkit.Uwp.SampleApp/SamplePages/KeyDownTriggerBehavior/KeyDownTriggerBehaviorXaml.bind
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:ani="using:Microsoft.Toolkit.Uwp.UI.Animations" | ||
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:interactivity="using:Microsoft.Xaml.Interactivity" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
mc:Ignorable="d"> | ||
|
||
<StackPanel | ||
VerticalAlignment="Center"> | ||
|
||
<TextBox | ||
PlaceholderText="Set the focus to this TextBox and press enter to trigger the animation" | ||
Width="500"> | ||
<interactivity:Interaction.Behaviors> | ||
<behaviors:KeyDownTriggerBehavior | ||
Key="Enter"> | ||
<behaviors:StartAnimationAction Animation="{Binding ElementName=MoveAnimation}" /> | ||
</behaviors:KeyDownTriggerBehavior> | ||
</interactivity:Interaction.Behaviors> | ||
</TextBox> | ||
|
||
<Button Background="Gray" Margin="0,40,0,0" Width="200" Height="200" HorizontalAlignment="Center" VerticalAlignment="Center"> | ||
<ani:Explicit.Animations> | ||
<ani:AnimationSet x:Name="MoveAnimation" IsSequential="True"> | ||
<ani:TranslationAnimation Duration="0:0:3" To="0,32,0" From="0,0,0" /> | ||
<ani:StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeOutAnimation}"/> | ||
<ani:StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeInAnimation}"/> | ||
<ani:TranslationAnimation Duration="0:0:1" To="0,0,0" From="0,32,0" /> | ||
</ani:AnimationSet> | ||
</ani:Explicit.Animations> | ||
|
||
<Image Source="ms-appx:///Assets/ToolkitLogo.png" Height="100" Width="100"> | ||
<ani:Explicit.Animations> | ||
<ani:AnimationSet x:Name="FadeOutAnimation"> | ||
<ani:OpacityAnimation From="1" | ||
To="0" | ||
Duration="0:0:1" | ||
Delay="0" | ||
EasingType="Linear" | ||
EasingMode="EaseOut"/> | ||
</ani:AnimationSet> | ||
<ani:AnimationSet x:Name="FadeInAnimation"> | ||
<ani:OpacityAnimation From="0" | ||
To="1" | ||
Duration="0:0:1" | ||
Delay="0" | ||
EasingType="Linear" | ||
EasingMode="EaseOut"/> | ||
</ani:AnimationSet> | ||
</ani:Explicit.Animations> | ||
</Image> | ||
|
||
</Button> | ||
|
||
</StackPanel> | ||
|
||
</Page> |
This file contains 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
This file contains 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
62 changes: 62 additions & 0 deletions
62
Microsoft.Toolkit.Uwp.UI.Behaviors/Keyboard/KeyDownTriggerBehavior.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.Xaml.Interactivity; | ||
using Windows.System; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Input; | ||
|
||
namespace Microsoft.Toolkit.Uwp.UI.Behaviors | ||
{ | ||
/// <summary> | ||
/// This behavior listens to a key down event on the associated <see cref="UIElement"/> when it is loaded and executes an action. | ||
/// </summary> | ||
[TypeConstraint(typeof(FrameworkElement))] | ||
public class KeyDownTriggerBehavior : Trigger<FrameworkElement> | ||
{ | ||
/// <summary> | ||
/// Identifies the <see cref="Key"/> property. | ||
/// </summary> | ||
public static readonly DependencyProperty KeyProperty = DependencyProperty.Register( | ||
nameof(Key), | ||
typeof(VirtualKey), | ||
typeof(KeyDownTriggerBehavior), | ||
new PropertyMetadata(null)); | ||
|
||
/// <summary> | ||
/// Gets or sets the key to listen when the associated object is loaded. | ||
/// </summary> | ||
public VirtualKey Key | ||
{ | ||
get => (VirtualKey)GetValue(KeyProperty); | ||
set => SetValue(KeyProperty, value); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void OnAttached() | ||
{ | ||
((FrameworkElement)AssociatedObject).KeyDown += OnAssociatedObjectKeyDown; | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void OnDetaching() | ||
{ | ||
((FrameworkElement)AssociatedObject).KeyDown -= OnAssociatedObjectKeyDown; | ||
} | ||
|
||
/// <summary> | ||
/// Invokes the current actions when the <see cref="Key"/> is pressed. | ||
/// </summary> | ||
/// <param name="sender">The source <see cref="UIElement"/> instance.</param> | ||
/// <param name="keyRoutedEventArgs">The arguments for the event (unused).</param> | ||
private void OnAssociatedObjectKeyDown(object sender, KeyRoutedEventArgs keyRoutedEventArgs) | ||
{ | ||
if (keyRoutedEventArgs.Key == Key) | ||
{ | ||
keyRoutedEventArgs.Handled = true; | ||
Interaction.ExecuteActions(sender, Actions, keyRoutedEventArgs); | ||
} | ||
} | ||
} | ||
} |