Skip to content

Commit bc58341

Browse files
committed
Merge branch 'main' into winui
# Conflicts: # CommunityToolkit.WinUI.SampleApp/SamplePages/AutoSelectBehavior/AutoSelectBehaviorXaml.bind # CommunityToolkit.WinUI.UI.Behaviors/Select/AutoSelectBehavior.cs # Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj # UnitTests/UnitTests.UWP/App.xaml # UnitTests/UnitTests.UWP/UnitTests.UWP.csproj
2 parents 80e388e + 6827aab commit bc58341

File tree

7 files changed

+114
-2
lines changed

7 files changed

+114
-2
lines changed
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:CommunityToolkit.WinUI.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>

CommunityToolkit.WinUI.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" />

CommunityToolkit.WinUI.SampleApp/SamplePages/samples.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
"Type": "MetadataControlPage",
139139
"Subcategory": "Status and Info",
140140
"About": "The control displays a list of metadata separated by bullets. The entries can either be strings or commands.",
141-
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI.Controls/MetadataControl",
141+
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/CommunityToolkit.WinUI.UI.Controls/MetadataControl",
142142
"XamlCodeFile": "MetadataControlCode.bind",
143143
"Icon": "/SamplePages/MetadataControl/MetadataControl.png",
144144
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/controls/MetadataControl.md"
@@ -820,6 +820,15 @@
820820
"Icon": "/Assets/Helpers.png",
821821
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/FocusBehaviors.md"
822822
},
823+
{
824+
"Name": "AutoSelectBehavior",
825+
"Subcategory": "Systems",
826+
"About": "Behavior to automatically select the entire content of a TextBox control when it loads",
827+
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/CommunityToolkit.WinUI.UI/Behaviors/",
828+
"XamlCodeFile": "/SamplePages/AutoSelectBehavior/AutoSelectBehaviorXaml.bind",
829+
"Icon": "/Assets/Helpers.png",
830+
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/AutoSelectBehavior.md"
831+
},
823832
{
824833
"Name": "Win2d Path Mini Language Parser",
825834
"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 Microsoft.UI.Xaml.Controls;
6+
7+
namespace CommunityToolkit.WinUI.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+
}

UnitTests/UnitTests.UWP/App.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:CommunityToolkit.WinUI.UI.Behaviors"
45
xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
5-
xmlns:ui="using:CommunityToolkit.WinUI.UI"
66
xmlns:helpers="using:UnitTests.Extensions.Helpers"
7+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
8+
xmlns:ui="using:CommunityToolkit.WinUI.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

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/UnitTests.UWP.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
</PackageReference>
5454
</ItemGroup>
5555
<ItemGroup>
56+
<Compile Include="Behaviors\Test_AutoSelectBehavior.cs" />
5657
<Compile Include="Converters\Test_AdaptiveHeightValueConverter.cs" />
5758
<Compile Include="Converters\Test_TaskResultConverter.cs" />
5859
<Compile Include="Converters\Test_BoolToObjectConverter.cs" />
@@ -189,6 +190,10 @@
189190
<Project>{b24a296c-b3eb-4e06-a64e-74ac2d1acc91}</Project>
190191
<Name>CommunityToolkit.WinUI.UI.Animations</Name>
191192
</ProjectReference>
193+
<ProjectReference Include="..\..\CommunityToolkit.WinUI.UI.Behaviors\CommunityToolkit.WinUI.UI.Behaviors.csproj">
194+
<Project>{d4ff799d-0df2-495a-adc9-3bbc4aef8971}</Project>
195+
<Name>CommunityToolkit.WinUI.UI.Behaviors</Name>
196+
</ProjectReference>
192197
<ProjectReference Include="..\..\CommunityToolkit.WinUI.UI.Controls.Layout\CommunityToolkit.WinUI.UI.Controls.Layout.csproj">
193198
<Project>{cb444381-18ba-4a51-bb32-3a498bcc1e99}</Project>
194199
<Name>CommunityToolkit.WinUI.UI.Controls.Layout</Name>

0 commit comments

Comments
 (0)