Expander

Expander控制項提供可擴充的容器來承載任何內容。 控制項有兩個主要屬性用來儲存你的內容:

此 Header 屬性可與任何視圖搭配使用,以實現完全自訂。 Header 會一律保持可見,而與其互動(點擊或輕觸)將會顯示或收合 Content。

備註

不建議在標頭內放置允許使用者互動的控制項。

Content

這是在與 Header 屬性互動(按一下或點按)時,或在 IsExpanded 屬性被修改時,會顯示的主要內容。

備註

Expander 如果置於 ListView 或 CollectionView中,可能會意外地發揮作用。 不過,您可以透過設定 public Action<TappedEventArgs>? HandleHeaderTapped { get; set; },以自訂實作加以控制。 每次點擊標頭時都會執行此動作。 請注意,變更此操作後,所有平台上的 CollectionView 和 ListView 可能會出現不同的行為表現。

展開器處於摺疊與展開狀態的螢幕擷取畫面

基本使用方式

以下範例示範如何使用 Expander 檢視:將 Header 屬性設為 Label 控制項,並將 Content 設為內含 Image 和 Label 的 HorizontalStackLayout。

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>

使用 Expander 功能

以下範例展示了如何在 XAML 中新增 Expander 檢視。

<toolkit:Expander>
    <toolkit:Expander.Header>
        <Label Text="Baboon"
               FontAttributes="Bold"
               FontSize="Medium" />
    </toolkit:Expander.Header>
    <HorizontalStackLayout Padding="10">
        <Image Source="http://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Papio_anubis_%28Serengeti%2C_2009%29.jpg/200px-Papio_anubis_%28Serengeti%2C_2009%29.jpg"
               Aspect="AspectFill"
               HeightRequest="120"
               WidthRequest="120" />
        <Label Text="Baboons are African and Arabian Old World monkeys belonging to the genus Papio, part of the subfamily Cercopithecinae."
               FontAttributes="Italic" />
    </HorizontalStackLayout>
</toolkit:Expander>

C#

以下範例展示了如何在 C# 中新增 Expander 檢視。

using CommunityToolkit.Maui.Views;

var expander = new Expander
{
    Header = new Label
    {
        Text = "Baboon",
        FontAttributes = FontAttributes.Bold,
        FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label))
    }
};

expander.Content = new HorizontalStackLayout
{
    Padding = new Thickness(10),

    Children =
    {
        new Image
        {
            Source = "http://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Papio_anubis_%28Serengeti%2C_2009%29.jpg/200px-Papio_anubis_%28Serengeti%2C_2009%29.jpg",
            Aspect = Aspect.AspectFill,
            HeightRequest = 120,
            WidthRequest = 120
        },

        new Label
        {
            Text = "Baboons are African and Arabian Old World monkeys belonging to the genus Papio, part of the subfamily Cercopithecinae.",
            FontAttributes = FontAttributes.Italic
        }
    }
};

C# 標記

using CommunityToolkit.Maui.Views;

Content = new Expander
{
    Header = new Label()
        .Text("Baboon")
        .Font(bold: true, size: 18),

    Content = new HorizontalStackLayout
    {
        new Image()
            .Source("http://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Papio_anubis_%28Serengeti%2C_2009%29.jpg/200px-Papio_anubis_%28Serengeti%2C_2009%29.jpg")
            .Size(120)
            .Aspect(Aspect.AspectFill),

        new Label()
            .Text("Baboons are African and Arabian Old World monkeys belonging to the genus Papio, part of the subfamily Cercopithecinae.")
            .Font(italic: true)

    }.Padding(10)

}.CenterHorizontal();

屬性

房產 類型 Description
Command ICommand 當 Expander 標頭被點選時執行。
CommandParameter object 傳遞給 Command的參數。
Direction ExpandDirection 定義了擴展方向。
Content IView? 定義展開時 Expander 要顯示的內容。
Header IView? 定義標頭內容。
IsExpanded bool 判斷 Expander 是否已展開。 此屬性使用 TwoWay binding 模式,預設值為 false。

ExpandDirection 列舉定義了下列成員:

價值觀 Description
Down 表示 Expander 內容在標頭下方。
Up 表示 Expander 內容位於標頭之上。

Expander 控制項也定義了 ExpandedChanged 事件,當點選 Expander 標頭時就會觸發。

展開變更事件引數

包含 ExpanderIsExpanded 狀態的事件參數。

屬性

房產 類型 Description
IsExpanded bool 判斷Expander是否已展開。

Examples

您可以在 .NET MAUI 社群工具組範例應用程式中找到這項功能的範例。

API

你可以在 Expander找到 的原始碼。