Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
]
},
"microsoft.visualstudio.slngen.tool": {
"version": "12.0.13",
"version": "12.0.32",
"commands": [
"slngen"
]
Expand Down
2 changes: 1 addition & 1 deletion components/Behaviors/samples/Behaviors.Samples.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project>
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.props))" Condition="Exists('$([MSBuild]::GetPathOfFileAbove(Directory.Build.props))')" />

<PropertyGroup>
Expand Down
6 changes: 6 additions & 0 deletions components/Behaviors/samples/Behaviors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions components/Behaviors/samples/SelectTextOnFocusBehaviorSample.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Page x:Class="BehaviorsExperiment.Samples.SelectTextOnFocusBehaviorSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviors="using:CommunityToolkit.WinUI.Behaviors"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity">

<StackPanel MaxWidth="480"
Spacing="8">
<TextBox Text="My content is not selected when focused" />
<TextBox Text="My content is selected when focused">
<interactivity:Interaction.Behaviors>
<behaviors:SelectTextOnFocusBehavior />
</interactivity:Interaction.Behaviors>
</TextBox>
</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 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();
}
}
1 change: 1 addition & 0 deletions components/Behaviors/src/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This package contains the following in the `CommunityToolkit.WinUI.Behaviors` na
- KeyDownTriggerBehavior
- NavigateToUriAction
- QuickReturnHeaderBehavior
- SelectTextOnFocusBehavior
- StartAnimationAction
- StackedNotificationBehavior
- StopAnimationAction
Expand Down
27 changes: 27 additions & 0 deletions components/Behaviors/src/Select/SelectTextOnFocusBehavior.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// This behavior automatically selects the text of the associated <see cref="TextBox"/> when it receives the focus.
/// </summary>
public sealed class SelectTextOnFocusBehavior : BehaviorBase<TextBox>
{
/// <inheritdoc/>
protected override bool Initialize()
{
AssociatedObject.GotFocus += OnAssociatedObjectGotFocus;
return base.Initialize();
}

/// <inheritdoc/>
protected override bool Uninitialize()
{
AssociatedObject.GotFocus -= OnAssociatedObjectGotFocus;
return base.Uninitialize();
}

private void OnAssociatedObjectGotFocus(object sender, RoutedEventArgs e) => AssociatedObject.SelectAll();
}
Loading