[WinUI 3][TextBox] TextBox の変更直前の状態を検知する

スポンサーリンク

この記事では TextBox が変更直前になったタイミングを知る(検知する)方法について説明をします。

公式サイト情報はコチラを参照してください。

環境

開発環境 Microsoft Visual Studio Enterprise 2019
Version 16.11.5
Framework Microsoft .NET Framewohttps://blog.hiros-dot.net/?p=11007rk Version 4.8.04161

TextBox の変更直前の状態を検知する

TextBox の変更直前の状態、つまり、入力が始まる前を検知するには BeforeTextChanging イベントを使用します。

以下は、TextBox のテキストが変更される直前に、「変更直前」というメッセージを表示する例です。

XAML の例

<Window
    x:Class="WinUISample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WinUISample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Orientation="Vertical" HorizontalAlignment="Center">

        <TextBox x:Name="myTextBox"
                 Width="150"
                 BeforeTextChanging="myTextBox_BeforeTextChanging" />

        <TextBlock x:Name="MyTextBlock" />

    </StackPanel>
</Window>

C# の例

private void myTextBox_BeforeTextChanging(TextBox sender, TextBoxBeforeTextChangingEventArgs args)
{
    MyTextBlock.Text = "変更直前";
}
テキストが変更される直前を検知する例

テキストが変更される直前を検知する例

WinUi Tips

本サイトでまとめている WinUI Tips の一覧はこちらから確認できます。

Please follow and like us:

コメント

タイトルとURLをコピーしました