次の方法で共有


SelectAllTextBehavior

SelectAllTextBehavior は、InputView がフォーカスされたときに、その中のすべてのテキスト (EntryEditor など) を選択する Behavior です。

重要

.NET MAUI Community Toolkit のビヘイビアーでは、ビヘイビアーの BindingContext は設定されません。ビヘイビアーはスタイルを利用して共有し、複数のコントロールに適用できるためです。 詳細については、「.NET MAUI のビヘイビアー」を参照してください

構文

次の例では、SelectAllTextBehaviorEntry に追加する方法を示します。

XAML

XAML 名前空間を含める

XAML でこのツールキットを使用するには、次の xmlns をページまたはビューに追加する必要があります。

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

したがって、以下のコードは、

<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>

次のように、xmlns を含むように変更されます。

<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>

SelectAllTextBehavior を使用する

SelectAllTextBehavior は、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#

SelectAllTextBehavior は、C# では次のように使用できます。

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

        var selectAllTextBehavior = new SelectAllTextBehavior();

        entry.Behaviors.Add(selectAllTextBehavior);

        Content = entry;
    }
}

C# Markup

この CommunityToolkit.Maui.Markup パッケージを使用すると、C# でこの Behavior をより簡潔な方法で使用できます。

using CommunityToolkit.Maui.Markup;

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

Note

MacCatalyst では、プラットフォーム固有の機能が原因で、“SelectAllText” の動作が機能するのは editor で右クリックを行う場合のみです。

このビヘイビアーの動作の例は .NET MAUI Community Toolkit サンプル アプリケーションで確認できます。