From caba7a78580b866c75ca4d859c1e9fb6c6ffcde6 Mon Sep 17 00:00:00 2001 From: Vincent Gromfeld Date: Mon, 16 Mar 2026 14:35:36 +0100 Subject: [PATCH 1/3] add select text on focus behavior --- .config/dotnet-tools.json | 2 +- .../samples/Behaviors.Samples.csproj | 2 +- components/Behaviors/samples/Behaviors.md | 6 +++++ .../SelectTextOnFocusBehaviorSample.xaml | 17 ++++++++++++ .../SelectTextOnFocusBehaviorSample.xaml.cs | 16 +++++++++++ .../src/Select/SelectTextOnFocusBehavior.cs | 27 +++++++++++++++++++ 6 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 components/Behaviors/samples/SelectTextOnFocusBehaviorSample.xaml create mode 100644 components/Behaviors/samples/SelectTextOnFocusBehaviorSample.xaml.cs create mode 100644 components/Behaviors/src/Select/SelectTextOnFocusBehavior.cs diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json index 644e6679..b31b0158 100644 --- a/.config/dotnet-tools.json +++ b/.config/dotnet-tools.json @@ -15,7 +15,7 @@ ] }, "microsoft.visualstudio.slngen.tool": { - "version": "12.0.13", + "version": "12.0.32", "commands": [ "slngen" ] diff --git a/components/Behaviors/samples/Behaviors.Samples.csproj b/components/Behaviors/samples/Behaviors.Samples.csproj index 54c8f0b2..37e484d2 100644 --- a/components/Behaviors/samples/Behaviors.Samples.csproj +++ b/components/Behaviors/samples/Behaviors.Samples.csproj @@ -1,4 +1,4 @@ - + diff --git a/components/Behaviors/samples/Behaviors.md b/components/Behaviors/samples/Behaviors.md index 6e507a66..02cdde6e 100644 --- a/components/Behaviors/samples/Behaviors.md +++ b/components/Behaviors/samples/Behaviors.md @@ -28,6 +28,12 @@ The AutoSelectBehavior automatically selects the entire content of its associate > [!Sample AutoSelectBehaviorSample] +## SelectTextOnFocusBehavior + +The SelectTextOnFocusBehavior automatically selects the entire content of its associated TextBox when it receives focus. + +> [!Sample SelectTextOnFocusBehaviorSample] + ## ViewportBehavior This behavior allows you to listen an element enter or exit the ScrollViewer viewport. diff --git a/components/Behaviors/samples/SelectTextOnFocusBehaviorSample.xaml b/components/Behaviors/samples/SelectTextOnFocusBehaviorSample.xaml new file mode 100644 index 00000000..7c8f4fd4 --- /dev/null +++ b/components/Behaviors/samples/SelectTextOnFocusBehaviorSample.xaml @@ -0,0 +1,17 @@ + + + + + + + + + + + + diff --git a/components/Behaviors/samples/SelectTextOnFocusBehaviorSample.xaml.cs b/components/Behaviors/samples/SelectTextOnFocusBehaviorSample.xaml.cs new file mode 100644 index 00000000..ea414ce8 --- /dev/null +++ b/components/Behaviors/samples/SelectTextOnFocusBehaviorSample.xaml.cs @@ -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 CommunityToolkit.WinUI.Behaviors; + +namespace BehaviorsExperiment.Samples; + +[ToolkitSample(id: nameof(SelectTextOnFocusBehaviorSample), nameof(SelectTextOnFocusBehavior), description: $"A sample for showing how to use the SelectTextOnFocusBehavior.")] +public sealed partial class SelectTextOnFocusBehaviorSample : Page +{ + public SelectTextOnFocusBehaviorSample() + { + this.InitializeComponent(); + } +} diff --git a/components/Behaviors/src/Select/SelectTextOnFocusBehavior.cs b/components/Behaviors/src/Select/SelectTextOnFocusBehavior.cs new file mode 100644 index 00000000..cb942d39 --- /dev/null +++ b/components/Behaviors/src/Select/SelectTextOnFocusBehavior.cs @@ -0,0 +1,27 @@ +// 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. + +namespace CommunityToolkit.WinUI.Behaviors; + +/// +/// This behavior automatically selects the text of the associated when it receives the focus. +/// +public sealed class SelectTextOnFocusBehavior : BehaviorBase +{ + /// + protected override bool Initialize() + { + AssociatedObject.GotFocus += OnAssociatedObjectGotFocus; + return base.Initialize(); + } + + /// + protected override bool Uninitialize() + { + AssociatedObject.GotFocus -= OnAssociatedObjectGotFocus; + return base.Uninitialize(); + } + + private void OnAssociatedObjectGotFocus(object sender, RoutedEventArgs e) => AssociatedObject.SelectAll(); +} From 04d4034ceee4e44c4814c8b22b1db1abb994618c Mon Sep 17 00:00:00 2001 From: Vincent Gromfeld Date: Mon, 16 Mar 2026 14:43:42 +0100 Subject: [PATCH 2/3] update readme --- components/Behaviors/src/ReadMe.md | 1 + 1 file changed, 1 insertion(+) diff --git a/components/Behaviors/src/ReadMe.md b/components/Behaviors/src/ReadMe.md index 7a6176b1..ed8aec2e 100644 --- a/components/Behaviors/src/ReadMe.md +++ b/components/Behaviors/src/ReadMe.md @@ -16,6 +16,7 @@ This package contains the following in the `CommunityToolkit.WinUI.Behaviors` na - KeyDownTriggerBehavior - NavigateToUriAction - QuickReturnHeaderBehavior +- SelectTextOnFocusBehavior - StartAnimationAction - StackedNotificationBehavior - StopAnimationAction From 888a013bf61d3f43d4b2111905ce72bc12ed0840 Mon Sep 17 00:00:00 2001 From: Vincent Gromfeld Date: Mon, 16 Mar 2026 14:52:21 +0100 Subject: [PATCH 3/3] xaml styling --- .../samples/SelectTextOnFocusBehaviorSample.xaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/components/Behaviors/samples/SelectTextOnFocusBehaviorSample.xaml b/components/Behaviors/samples/SelectTextOnFocusBehaviorSample.xaml index 7c8f4fd4..723929d9 100644 --- a/components/Behaviors/samples/SelectTextOnFocusBehaviorSample.xaml +++ b/components/Behaviors/samples/SelectTextOnFocusBehaviorSample.xaml @@ -1,11 +1,11 @@ - + - +