[WinUI 3][Expander] 開閉されたことを知る

スポンサーリンク

この記事では Expander が開閉されたことを知る方法について説明します。

環境

開発環境 Microsoft Visual Studio Enterprise 2019
Version 16.11.5
Framework Microsoft .NET Framework Version 4.8.04161

Expander が開閉されたことを知る

Expander が開閉されたことを知るには、Collapsed イベント Expanded イベントを使用します。Expander が閉じられた時は Collapsed イベントが発生し、開かれた時は Expanding イベントが発生します。

以下は、Expander の Collapsed イベントと Expanding イベントを使用する例です。開閉の状態合わせて、TextBlock にメッセージを表示します。

XAML の例

<Window
    x:Class="WinUIAppSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WinUIAppSample"
    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" VerticalAlignment="Center">
        <Expander x:Name="myExpander1" Header="News1" 
                  Collapsed="myExpander1_Collapsed"
                  Expanding="myExpander1_Expanding">
            <TextBlock Text="雨ニモ負ケズ風ニモ負ケズ"
                       Foreground="Blue"/>
        </Expander>
        <TextBlock x:Name="myTextBlock" />
    </StackPanel>
</Window>

C#の例

private void myExpander1_Collapsed(Expander sender, ExpanderCollapsedEventArgs args)
{
    myTextBlock.Text = "閉じられました";
}

private void myExpander1_Expanding(Expander sender, ExpanderExpandingEventArgs args)
{
    myTextBlock.Text = "開かれました";
}
開閉されたことを知る例

開閉されたことを知る例

Please follow and like us:

コメント

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