-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New behavior KeyDownTriggerBehavior #4160
Changes from 1 commit
f9541bf
7513e42
fc4e6d7
6c5f8a2
368b8a0
b1905ce
3f0df53
e2e5a65
8b3be50
eab041e
bd052bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<Page x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.KeyDownTriggerBehaviorPage" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
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" | ||
xmlns:animations="using:Microsoft.Toolkit.Uwp.UI.Animations" | ||
mc:Ignorable="d"> | ||
|
||
<StackPanel | ||
VerticalAlignment="Center"> | ||
|
||
<TextBox | ||
PlaceholderText="Set the focus to this TextBox and press enter to trigger the animation" | ||
Width="300"> | ||
<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"> | ||
<animations:Explicit.Animations> | ||
<animations:AnimationSet x:Name="MoveAnimation" IsSequential="True"> | ||
<animations:TranslationAnimation Duration="0:0:3" To="0,32,0" From="0,0,0" /> | ||
<animations:StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeOutAnimation}" /> | ||
<animations:StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeInAnimation}" /> | ||
<animations:TranslationAnimation Duration="0:0:1" To="0,0,0" From="0,32,0" /> | ||
</animations:AnimationSet> | ||
</animations:Explicit.Animations> | ||
|
||
<Image Source="ms-appx:///Assets/ToolkitLogo.png" Height="100" Width="100"> | ||
<animations:Explicit.Animations> | ||
<animations:AnimationSet x:Name="FadeOutAnimation"> | ||
<animations:OpacityAnimation From="1" | ||
To="0" | ||
Duration="0:0:1" | ||
Delay="0" | ||
EasingType="Linear" | ||
EasingMode="EaseOut" /> | ||
</animations:AnimationSet> | ||
<animations:AnimationSet x:Name="FadeInAnimation"> | ||
<animations:OpacityAnimation From="0" | ||
To="1" | ||
Duration="0:0:1" | ||
Delay="0" | ||
EasingType="Linear" | ||
EasingMode="EaseOut" /> | ||
</animations:AnimationSet> | ||
</animations:Explicit.Animations> | ||
</Image> | ||
|
||
</Button> | ||
|
||
</StackPanel> | ||
|
||
</Page> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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 Windows.UI.Xaml.Controls; | ||
|
||
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages | ||
{ | ||
/// <summary> | ||
/// A page that shows how to use the AutoFocusBehavior | ||
/// </summary> | ||
public sealed partial class KeyDownTriggerBehaviorPage : Page | ||
{ | ||
public KeyDownTriggerBehaviorPage() => InitializeComponent(); | ||
ArchieCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
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="300"> | ||
<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"> | ||
<Explicit.Animations> | ||
<AnimationSet x:Name="MoveAnimation" IsSequential="True"> | ||
<TranslationAnimation Duration="0:0:3" To="0,32,0" From="0,0,0" /> | ||
<StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeOutAnimation}" /> | ||
<StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeInAnimation}" /> | ||
<TranslationAnimation Duration="0:0:1" To="0,0,0" From="0,32,0" /> | ||
</AnimationSet> | ||
</Explicit.Animations> | ||
|
||
<Image Source="ms-appx:///Assets/ToolkitLogo.png" Height="100" Width="100"> | ||
<Explicit.Animations> | ||
<AnimationSet x:Name="FadeOutAnimation"> | ||
<OpacityAnimation From="1" | ||
To="0" | ||
Duration="0:0:1" | ||
Delay="0" | ||
EasingType="Linear" | ||
EasingMode="EaseOut" /> | ||
</AnimationSet> | ||
<AnimationSet x:Name="FadeInAnimation"> | ||
<OpacityAnimation From="0" | ||
To="1" | ||
Duration="0:0:1" | ||
Delay="0" | ||
EasingType="Linear" | ||
EasingMode="EaseOut" /> | ||
</AnimationSet> | ||
</Explicit.Animations> | ||
</Image> | ||
|
||
</Button> | ||
|
||
</StackPanel> | ||
|
||
</Page> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -785,7 +785,7 @@ | |
"Type": "ThemeListenerPage", | ||
"Subcategory": "Systems", | ||
"About": "The ThemeListener allows you to keep track of changes to the System Theme.", | ||
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs", | ||
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs", | ||
"Icon": "/Assets/Helpers.png", | ||
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/helpers/ThemeListener.md" | ||
}, | ||
|
@@ -839,12 +839,22 @@ | |
"Icon": "/Assets/Helpers.png", | ||
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/AutoSelectBehavior.md" | ||
}, | ||
{ | ||
"Name": "KeyDownTriggerBehavior", | ||
"Type": "KeyDownTriggerBehaviorPage", | ||
"Subcategory": "Systems", | ||
"About": "Behavior to listen to a key press on a control and executes actions", | ||
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI.Behaviors/Keyboard/KeyDownTriggerBehavior.cs", | ||
"XamlCodeFile": "KeyDownTriggerBehaviorXaml.bind", | ||
ArchieCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Icon": "/Assets/Helpers.png", | ||
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/KeyboardBehaviors.md" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure this DocumentationUrl property points to the correct markdown file, for the corresponding file in the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @XAML-Knight can I just remove the DocumentationUrl? This behavior is so simple that the existing comments in the code is self-explanatory. So far, it took me like 20 mins to do the behavior, but all the rest is many hours! |
||
}, | ||
{ | ||
"Name": "Win2d Path Mini Language Parser", | ||
"Type": "CanvasPathGeometryPage", | ||
"Subcategory": "Parser", | ||
"About": "CanvasPathGeometry class allows you to convert Win2d Path Mini Language string to CanvasGeometry, Brushes, CanvasStrokes or CanvasStrokeStyles.", | ||
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.UI.Media/Geometry", | ||
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.UI.Media/Geometry", | ||
"Icon": "/SamplePages/CanvasPathGeometry/CanvasPathGeometry.png", | ||
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/parsers/CanvasPathGeometry.md" | ||
}, | ||
|
@@ -882,15 +892,15 @@ | |
"Name": "Guard APIs", | ||
"Subcategory": "Developer", | ||
"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.", | ||
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Diagnostics", | ||
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Diagnostics", | ||
"Icon": "/Assets/Helpers.png", | ||
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/developer-tools/Guard.md" | ||
}, | ||
{ | ||
"Name": "High Performance APIs", | ||
"Subcategory": "Developer", | ||
"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.", | ||
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.HighPerformance", | ||
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.HighPerformance", | ||
"Icon": "/Assets/Helpers.png", | ||
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/high-performance/Introduction.md" | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Windows.Input; | ||
ArchieCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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<UIElement> | ||
ArchieCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
|
||
/// <summary> | ||
/// The DP to store the <see cref="Key"/> property value. | ||
ArchieCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// </summary> | ||
public static readonly DependencyProperty KeyProperty = DependencyProperty.Register( | ||
"Key", | ||
ArchieCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} | ||
|
||
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( | ||
ArchieCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Command", | ||
typeof(ICommand), | ||
typeof(KeyDownTriggerBehavior), | ||
new PropertyMetadata(null)); | ||
|
||
/// <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) | ||
ArchieCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
keyRoutedEventArgs.Handled = true; | ||
Interaction.ExecuteActions(sender, Actions, keyRoutedEventArgs); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this to minimize noise.