[WinUI 3][Expander] 開閉状態を制御する

スポンサーリンク

この記事では Expander の開閉を制御する方法について説明します。

環境

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

開閉方向を制御する

Expander の開閉方向を制御するには IsExpanded プロパティを使用します。開いた状態にする場合は True を設定します。既定値は False となっており閉じられれた状態になります。

以下は、IsExpanded プロパティを使用するXAML と C# の例です。上の Expander は開いた状態にし、下の Expander は閉じた状態にします。

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" 
                  IsExpanded="True">
            <TextBlock Text="雨ニモ負ケズ風ニモ負ケズ"
                       Foreground="Blue"/>
        </Expander>
        <Expander x:Name="myExpander2" Header="News2"
                  IsExpanded="False">
            <TextBlock Text="雨ニモ負ケズ風ニモ負ケズ" 
                       Foreground="Blue"/>
        </Expander>
    </StackPanel>
</Window>

C# の例

public MainWindow()
{
    this.InitializeComponent();
    myExpander1.IsExpanded = true;
    myExpander2.IsExpanded = false;
}
IsExpanded プロパティの使用例

IsExpanded プロパティの使用例

Please follow and like us:

コメント

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