本記事の概要
この記事では Editor で任意の文字列を選択する方法について説明をします。
公式サイト情報はコチラを参照してください。
任意の文字列を選択する
Editor で任意の文字列を選択するには CursotPosition プロパティと、SelectionLength プロパティを使用します。CursorPosition プロパティは Editor 上のカーソルの位置を表し、既定値は 0 です。過去記事で説明していますので参照ください。もう1つの SelectionLength は何文字選択するかを表し、既定値は 0です。
XAML の例
XAML で Editor に入力されている文字列を選択する例を以下に示します。
この例では、「Hello .NET MAUI」という文字列のうち、先頭から6文字目にカーソルを置き、4文字選択しますので「.NET」が選択状態になります。
XAML の例
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiAppSample.MainPage">
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Editor Text="Hello .NET MAUI"
CursorPosition="6"
SelectionLength="4" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
C# の例
以下は 先ほどのXAML の例を コードビハインドで行う例です。
C# の例
public MainPage()
{
InitializeComponent();
myEditor1.CursorPosition = 6;
myEditor1.SelectionLength = 4;
}
XAML の例
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiAppSample.MainPage">
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Editor x:Name="myEditor1" Text="Hello .NET MAUI" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
.NET MAUI Tips
本サイトでまとめている .NET MAUI Tips の一覧はこちらから確認できます。
Please follow and like us:


コメント