Skip to content

Commit 6827aab

Browse files
author
msftbot[bot]
authored
Add AutoSelectBehavior (#3786)
## Fixes #3790 Introduce a new AutoSelect behavior that automatically selects the entire content of its associated TextBox when it is loaded. ## PR Type What kind of change does this PR introduce? Feature Sample app changes ## What is the current behavior? N/A ## What is the new behavior? N/A ## PR Checklist Please check if your PR fulfills the following requirements: - [x] Tested code with current [supported SDKs](../readme.md#supported) - [x] Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: MicrosoftDocs/WindowsCommunityToolkitDocs#525 - [x] Sample in sample app has been added / updated (for bug fixes / features) - [ ] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets) - [ ] New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/windows-toolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc... - [x] Tests for the changes have been added (for bug fixes / features) (if applicable) - [x] Header has been added to all new source files (run *build/UpdateHeaders.bat*) - [x] Contains **NO** breaking changes ## Other information N/A
2 parents e8029ec + d38bd78 commit 6827aab

File tree

8 files changed

+114
-1
lines changed

8 files changed

+114
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@
622622
<Content Include="SamplePages\VisualEffectFactory\VisualEffectFactory.bind" />
623623
<Content Include="SamplePages\Animations\Activities\InvokeActionsActivityCode.bind" />
624624
<Content Include="SamplePages\Animations\Activities\StartAnimationActivityCode.bind" />
625+
<Content Include="SamplePages\AutoSelectBehavior\AutoSelectBehaviorXaml.bind" />
625626
<Content Include="SamplePages\Graph\LoginButtonXaml.bind" />
626627
<Content Include="SamplePages\Graph\PeoplePickerXaml.bind" />
627628
<Content Include="SamplePages\Graph\PersonViewXaml.bind" />
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
<Grid>
10+
<StackPanel HorizontalAlignment="Center"
11+
VerticalAlignment="Center">
12+
<TextBox Text="My content is not selected when loaded" />
13+
<TextBox Text="My content is selected when loaded">
14+
<interactivity:Interaction.Behaviors>
15+
<behaviors:AutoSelectBehavior />
16+
</interactivity:Interaction.Behaviors>
17+
</TextBox>
18+
</StackPanel>
19+
</Grid>
20+
</Page>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/XamlOnlyPage.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<triggers:UserHandPreferenceStateTrigger x:Key="UserHandPreferenceStateTrigger" />
3333
<triggers:UserInteractionModeStateTrigger x:Key="UserInteractionModeStateTrigger" />
3434
<behaviors:StartAnimationAction x:Key="StartAnimationAction" />
35+
<behaviors:AutoSelectBehavior x:Key="AutoSelectBehavior" />
3536
<controls:ColorPicker x:Key="ColorPicker" />
3637
<controls:ColorPickerButton x:Key="ColorPickerButton" />
3738
<primitives:ColorPickerSlider x:Key="ColorPickerSlider" />

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,15 @@
821821
"Icon": "/Assets/Helpers.png",
822822
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/FocusBehaviors.md"
823823
},
824+
{
825+
"Name": "AutoSelectBehavior",
826+
"Subcategory": "Systems",
827+
"About": "Behavior to automatically select the entire content of a TextBox control when it loads",
828+
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI/Behaviors/",
829+
"XamlCodeFile": "/SamplePages/AutoSelectBehavior/AutoSelectBehaviorXaml.bind",
830+
"Icon": "/Assets/Helpers.png",
831+
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/AutoSelectBehavior.md"
832+
},
824833
{
825834
"Name": "Win2d Path Mini Language Parser",
826835
"Type": "CanvasPathGeometryPage",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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.UI.Behaviors
8+
{
9+
/// <summary>
10+
/// This behavior automatically selects the entire content of the associated <see cref="TextBox"/> when it is loaded.
11+
/// </summary>
12+
public sealed class AutoSelectBehavior : BehaviorBase<TextBox>
13+
{
14+
/// <inheritdoc/>
15+
protected override void OnAssociatedObjectLoaded() => AssociatedObject.SelectAll();
16+
}
17+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 FluentAssertions;
6+
using Microsoft.Toolkit.Uwp;
7+
using Microsoft.Toolkit.Uwp.UI;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
using System.Threading.Tasks;
10+
using Windows.UI.Xaml.Controls;
11+
using Windows.UI.Xaml.Markup;
12+
13+
namespace UnitTests.Behaviors
14+
{
15+
[TestClass]
16+
public class Test_AutoSelectBehavior : VisualUITestBase
17+
{
18+
[TestCategory("Behaviors")]
19+
[TestMethod]
20+
public async Task Test_AutoSelectBehavior_SelectsAllContent()
21+
{
22+
await App.DispatcherQueue.EnqueueAsync(async () =>
23+
{
24+
var treeRoot = XamlReader.Load(@"<Page
25+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
26+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
27+
xmlns:behaviors=""using:Microsoft.Toolkit.Uwp.UI.Behaviors""
28+
xmlns:interactivity=""using:Microsoft.Xaml.Interactivity"">
29+
<Grid>
30+
<TextBox x:Name=""TargetElement"" Text=""ThisShouldBeSelectedWhenLoaded"">
31+
<interactivity:Interaction.Behaviors>
32+
<behaviors:AutoSelectBehavior />
33+
</interactivity:Interaction.Behaviors>
34+
</TextBox>
35+
</Grid>
36+
</Page>") as Page;
37+
38+
// Test Setup
39+
Assert.IsNotNull(treeRoot, "XAML Failed to Load");
40+
41+
// Initialize Visual Tree
42+
await SetTestContentAsync(treeRoot);
43+
44+
// Main Test
45+
var textBox = treeRoot.FindName("TargetElement") as TextBox;
46+
47+
Assert.IsNotNull(textBox, "TextBox was not found in the tree.");
48+
textBox.SelectedText.Should().Be("ThisShouldBeSelectedWhenLoaded");
49+
});
50+
}
51+
}
52+
}

UnitTests/UnitTests.UWP/UnitTestApp.xaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<Application x:Class="UnitTests.App"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
45
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
5-
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
66
xmlns:helpers="using:UnitTests.Extensions.Helpers"
7+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
8+
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
79
xmlns:unitTestExtensions="using:UnitTests.Extensions"
810
RequestedTheme="Light">
911
<Application.Resources>
@@ -35,6 +37,12 @@
3537
</Button.Flyout>
3638
</Button>
3739

40+
<TextBox x:Key="DummyTextBox">
41+
<interactivity:Interaction.Behaviors>
42+
<behaviors:AutoSelectBehavior />
43+
</interactivity:Interaction.Behaviors>
44+
</TextBox>
45+
3846
<Style TargetType="controls:UniformGrid" />
3947
<ui:NullableBoolExtension x:Key="nullableBool" />
4048

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
</PackageReference>
132132
</ItemGroup>
133133
<ItemGroup>
134+
<Compile Include="Behaviors\Test_AutoSelectBehavior.cs" />
134135
<Compile Include="Converters\Test_AdaptiveHeightValueConverter.cs" />
135136
<Compile Include="Converters\Test_TaskResultConverter.cs" />
136137
<Compile Include="Converters\Test_BoolToObjectConverter.cs" />
@@ -268,6 +269,10 @@
268269
<Project>{b24a296c-b3eb-4e06-a64e-74ac2d1acc91}</Project>
269270
<Name>Microsoft.Toolkit.Uwp.UI.Animations</Name>
270271
</ProjectReference>
272+
<ProjectReference Include="..\..\Microsoft.Toolkit.Uwp.UI.Behaviors\Microsoft.Toolkit.Uwp.UI.Behaviors.csproj">
273+
<Project>{d4ff799d-0df2-495a-adc9-3bbc4aef8971}</Project>
274+
<Name>Microsoft.Toolkit.Uwp.UI.Behaviors</Name>
275+
</ProjectReference>
271276
<ProjectReference Include="..\..\Microsoft.Toolkit.Uwp.UI.Controls.Layout\Microsoft.Toolkit.Uwp.UI.Controls.Layout.csproj">
272277
<Project>{cb444381-18ba-4a51-bb32-3a498bcc1e99}</Project>
273278
<Name>Microsoft.Toolkit.Uwp.UI.Controls.Layout</Name>

0 commit comments

Comments
 (0)