Redigera

Dela via


SelectAllTextBehavior

The SelectAllTextBehavior is a Behavior that will select all text in an InputView (e.g. an Entry or Editor) when it becomes focused.

Important

The .NET MAUI Community Toolkit Behaviors do not set the BindingContext of a behavior, because behaviors can be shared and applied to multiple controls through styles. For more information refer to .NET MAUI Behaviors

Syntax

The following examples show how to add the SelectAllTextBehavior to an Entry.

XAML

Including the XAML namespace

In order to use the toolkit in XAML the following xmlns needs to be added into your page or view:

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

Therefore the following:

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

</ContentPage>

Would be modified to include the xmlns as follows:

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">

</ContentPage>

Using the SelectAllTextBehavior

The SelectAllTextBehavior can be used as follows in XAML:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="CommunityToolkit.Maui.Sample.Pages.Behaviors.SelectAllTextBehaviorPage">

    <Entry>
        <Entry.Behaviors>
            <toolkit:SelectAllTextBehavior />
        </Entry.Behaviors>
    </Entry>

</ContentPage>

C#

The SelectAllTextBehavior can be used as follows in C#:

class SelectAllTextBehaviorPage : ContentPage
{
    public SelectAllTextBehaviorPage()
    {
        var entry = new Entry();

        var selectAllTextBehavior = new SelectAllTextBehavior();

        entry.Behaviors.Add(selectAllTextBehavior);

        Content = entry;
    }
}

C# Markup

Our CommunityToolkit.Maui.Markup package provides a much more concise way to use this Behavior in C#.

using CommunityToolkit.Maui.Markup;

class SelectAllTextBehaviorPage : ContentPage
{
    public SelectAllTextBehaviorPage()
    {
        Content = new Entry()
            .Behaviors(new SelectAllTextBehavior());
    }
}

Note

On MacCatalyst, the behavior “SelectAllText” only works by performing a right-click in the editor due to platform specific functionality.

Examples

You can find an example of this behavior in action in the .NET MAUI Community Toolkit Sample Application.