MenuFlyout 類別

定義

表示顯示命令功能表的飛出視窗。

/// [Microsoft.UI.Xaml.Markup.ContentProperty(Name="Items")]
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class MenuFlyout : FlyoutBase
[Microsoft.UI.Xaml.Markup.ContentProperty(Name="Items")]
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public class MenuFlyout : FlyoutBase
Public Class MenuFlyout
Inherits FlyoutBase
<MenuFlyout>
  oneOrMoreItems
</MenuFlyout>
繼承
Object IInspectable DependencyObject FlyoutBase MenuFlyout
衍生
屬性

範例

提示

如需詳細資訊、設計指引和程式碼範例,請參閱 功能表和操作功能表

WinUI 3 資源庫應用程式包含大部分 WinUI 3 控制件、特性和功能的互動式範例。 從 Microsoft Store 取得應用程式,或在 GitHub 上取得原始程式碼。

這個範例會建立 MenuFlyout 類別,並使用 ContextFlyout 屬性 (適用於大多數控制項的屬性),以將 MenuFlyout 類別顯示為操作功能表。

<Rectangle Height="100" Width="100">
  <Rectangle.ContextFlyout>
    <MenuFlyout>
      <MenuFlyoutItem Text="Change color" Click="ChangeColorItem_Click" />
    </MenuFlyout>
  </Rectangle.ContextFlyout>
  <Rectangle.Fill>
    <SolidColorBrush x:Name="rectangleFill" Color="Red" />
  </Rectangle.Fill>
</Rectangle>
private void ChangeColorItem_Click(object sender, RoutedEventArgs e)
{
    // Change the color from red to blue or blue to red.
    if (rectangleFill.Color == Windows.UI.Colors.Red)
    {
        rectangleFill.Color = Windows.UI.Colors.Blue;
    }
    else
    {
        rectangleFill.Color = Windows.UI.Colors.Red;
    }
}

下一個範例幾乎完全相同,但不會使用 ContextFlyout 屬性來將 MenuFlyout 類別顯示為操作功能表,這個範例改用 FlyoutBase.ShowAttachedFlyout 屬性來將它顯示為功能表。

<Rectangle
  Height="100" Width="100"
  Tapped="Rectangle_Tapped">
  <FlyoutBase.AttachedFlyout>
    <MenuFlyout>
      <MenuFlyoutItem Text="Change color" Click="ChangeColorItem_Click" />
    </MenuFlyout>
  </FlyoutBase.AttachedFlyout>
  <Rectangle.Fill>
    <SolidColorBrush x:Name="rectangleFill" Color="Red" />
  </Rectangle.Fill>
</Rectangle>
private void Rectangle_Tapped(object sender, TappedRoutedEventArgs e)
{
    FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
}

private void ChangeColorItem_Click(object sender, RoutedEventArgs e)
{
    // Change the color from red to blue or blue to red.
    if (rectangleFill.Color == Windows.UI.Colors.Red)
    {
        rectangleFill.Color = Windows.UI.Colors.Blue;
    }
    else
    {
        rectangleFill.Color = Windows.UI.Colors.Red;
    }
}

此範例示範如何根據應用程式中的變更條件,在運行時間新增和移除功能表項。

<StackPanel Margin="40" Width="220">
    <Rectangle x:Name="Rect1" Height="100" Width="200" 
               Stroke="Black" StrokeThickness="1" Fill="White">
        <Rectangle.ContextFlyout>
            <MenuFlyout x:Name="RectangleColorMenu"/>
        </Rectangle.ContextFlyout>
    </Rectangle>

    <StackPanel>
        <TextBlock TextWrapping="WrapWholeWords"
                   Text="Check colors to include in the menu, then choose a color from the context menu on the rectangle."/>
        <CheckBox Content="Blue" Click="CheckBox_Click" Tag="blue"/>
        <CheckBox Content="Green" Click="CheckBox_Click" Tag="green"/>
        <CheckBox Content="Red" Click="CheckBox_Click" Tag="red"/>
        <CheckBox Content="Yellow" Click="CheckBox_Click" Tag="yellow"/>
    </StackPanel>
</StackPanel>
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
    // Using the Tag property lets you localize the display name
    // without affecting functionality.
    var cb = (CheckBox)sender;
    if (cb.IsChecked == true)
    {
        AddMenuItem(cb.Tag.ToString(), cb.Content.ToString());
    }
    else
    {
        RemoveMenuItem(cb.Content.ToString());
    }
}

private void AddMenuItem(string colorString, string locColorName)
{
    // Set the color.
    Color newColor = Colors.Blue;
    if (colorString == "green")
        newColor = Colors.Green;
    else if (colorString == "red")
        newColor = Colors.Red;
    else if (colorString == "yellow")
        newColor = Colors.Yellow;

    // Create the menu item.
    var newMenuItem = new MenuFlyoutItem();
    newMenuItem.Text = locColorName;
    newMenuItem.Click += (s, e1) =>
    {
        Rect1.Fill = new SolidColorBrush(newColor);
    };

    // Add the item to the menu.
    RectangleColorMenu.Items.Add(newMenuItem);

    // Sort the menu so it's always consistent.
    var orderedItems =  RectangleColorMenu.Items.OrderBy(i => ((MenuFlyoutItem)i).Text).ToList();
    RectangleColorMenu.Items.Clear();
    foreach (var item in orderedItems)
    {
        RectangleColorMenu.Items.Add(item);
    }
}

private void RemoveMenuItem(string locColorName)
{
    // Get any menu items to remove and remove them.
    var items = RectangleColorMenu.Items.Where(i => ((MenuFlyoutItem)i).Text == locColorName);
    foreach (MenuFlyoutItem item in items)
    {
        RectangleColorMenu.Items.Remove(item);
    }
}

備註

提示

如需詳細資訊、設計指引和程式碼範例,請參閱 功能表和操作功能表

MenuFlyout 會暫時顯示與使用者目前正在執行的動作相關的命令或選項清單。

功能表飛出視窗控制項

使用 飛出視窗 控件來顯示單一專案,並使用 MenuFlyout 控件來顯示項目的功能表。 如需詳細資訊,請參閱 功能表和操作功能表

MenuFlyout 控件可作為 Button.Flyout 屬性的值。 這通常會在 XAML 中設定為頁面 UI 定義的一部分。 Button 是唯一具有專用 飛出視窗 屬性的控件。 當設定為 Button.Flyout 時,MenuFlyout 會在點選或叫用按鈕時顯示。

若要將 MenuFlyout 與其他控件關聯為內容功能表,請使用任何 UIElement 上可用的 ContextFlyout 屬性。

您可以使用 FlyoutBase.AttachedFlyout 附加屬性,將 MenuFlyout 與其他控件產生關聯,做為一般功能表。 使用 FlyoutBase.AttachedFlyout 將 MenuFlyout 指派給其他 UI 元素時,您必須呼叫 ShowAt 方法或靜態 ShowAttachedFlyout 方法來顯示飛出視窗。

控件樣式和範本

您可以修改預設 的 StyleControlTemplate ,讓控件具有唯一的外觀。 如需修改控件樣式和範本的相關信息,請參閱 XAML 樣式。 定義控件外觀的預設樣式、範本和資源會包含在檔案中 generic.xaml 。 為了設計目的,generic.xaml會與 Windows 應用程式 SDK NuGet 套件一起安裝。 根據預設,此位置為 \Users\<username>\.nuget\packages\microsoft.windowsappsdk\<version>\lib\uap10.0\Microsoft.UI\Themes\generic.xaml。 不同 SDK 版本的樣式和資源可能會有不同的值。

XAML 也包含資源,可讓您用來修改不同視覺狀態中控件的色彩,而不需修改控件範本。 修改這些資源是慣用的設定屬性,例如 BackgroundForeground。 如需詳細資訊,請參閱 XAML 樣式文章的輕量型樣式一節。

建構函式

MenuFlyout()

初始化 MenuFlyout 類別的新實例。

屬性

AllowFocusOnInteraction

取得或設定值,這個值表示當使用者與其互動時,專案是否會自動取得焦點。

(繼承來源 FlyoutBase)
AllowFocusWhenDisabled

取得或設定值,指定控件在停用時是否可以接收焦點。

(繼承來源 FlyoutBase)
AreOpenCloseAnimationsEnabled

取得或設定值,這個值表示當飛出窗口開啟或關閉時,是否播放動畫。

(繼承來源 FlyoutBase)
Dispatcher

一律會在 Windows 應用程式 SDK 應用程式中傳null回。 請改用 DispatcherQueue

(繼承來源 DependencyObject)
DispatcherQueue

DispatcherQueue取得與這個 物件相關聯的 。 DispatcherQueue表示即使程式代碼是由非UI線程起始,也可以存取 DependencyObject UI線程上的設備。

(繼承來源 DependencyObject)
ElementSoundMode

取得或設定值,指定控件是否播放音效的喜好設定。

(繼承來源 FlyoutBase)
InputDevicePrefersPrimaryCommands

取得值,指出用來開啟飛出視窗的輸入設備是否不容易開啟次要命令。

(繼承來源 FlyoutBase)
IsConstrainedToRootBounds

取得值,這個值表示飛出視窗是否顯示在 XAML 根目錄的界限內。 對於 Windows 應用程式 SDK 應用程式而言,此屬性一律為 true

(繼承來源 FlyoutBase)
IsOpen

取得值,這個值表示飛出視窗是否開啟。

(繼承來源 FlyoutBase)
Items

取得用來產生功能表內容的集合。

LightDismissOverlayMode

取得或設定值,這個值會指定 淺色關閉 UI 外部的區域是否變深。

(繼承來源 FlyoutBase)
MenuFlyoutPresenterStyle

取得或設定轉譯 MenuFlyout 時所使用的樣式。

MenuFlyoutPresenterStyleProperty

識別 MenuFlyoutPresenterStyle 相依性屬性。

OverlayInputPassThroughElement

取得或設定即使飛出視窗重疊下方的指標輸入事件,也應該接收指標輸入事件的專案。

(繼承來源 FlyoutBase)
Placement

取得或設定要用於飛出視窗的預設放置位置,相對於其放置目標。

(繼承來源 FlyoutBase)
ShouldConstrainToRootBounds

取得或設定值,這個值表示是否應該在 XAML 根目錄的界限內顯示飛出視窗。

(繼承來源 FlyoutBase)
ShowMode

取得或設定值,這個值表示顯示飛出窗口的行為。

(繼承來源 FlyoutBase)
SystemBackdrop

取得或設定要套用至這個飛出視窗的系統底線。 飛出視窗內容後方會轉譯該底板。

(繼承來源 FlyoutBase)
Target

取得要當做飛出視窗放置目標的 元素。

(繼承來源 FlyoutBase)
XamlRoot

取得或設定檢視這個飛出視窗的 XamlRoot。

(繼承來源 FlyoutBase)

方法

ClearValue(DependencyProperty)

清除相依性屬性的本機值。

(繼承來源 DependencyObject)
CreatePresenter()

在衍生類別中覆寫時,初始化 控件,以針對衍生控件顯示飛出視窗內容。 注意:這個方法沒有基類實作,而且必須在衍生類別中覆寫。

(繼承來源 FlyoutBase)
GetAnimationBaseValue(DependencyProperty)

傳回為相依性屬性建立的任何基底值,如果動畫未使用中,則適用此屬性。

(繼承來源 DependencyObject)
GetValue(DependencyProperty)

DependencyObject 傳回相依性屬性的目前有效值。

(繼承來源 DependencyObject)
Hide()

關閉飛出視窗。

(繼承來源 FlyoutBase)
OnProcessKeyboardAccelerators(ProcessKeyboardAcceleratorEventArgs)

在應用程式中處理快捷鍵 (快捷鍵) 之前呼叫。 每當應用程式程式代碼或內部進程呼叫 ProcessKeyboardAccelerators 時叫用。 覆寫此方法以影響預設加速器處理。

(繼承來源 FlyoutBase)
ReadLocalValue(DependencyProperty)

如果已設定本機值,則傳回相依性屬性的本機值。

(繼承來源 DependencyObject)
RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback)

註冊通知函式,以接聽此 DependencyObject 實例上特定 DependencyProperty 的變更。

(繼承來源 DependencyObject)
SetValue(DependencyProperty, Object)

設定 DependencyObject 上相依性屬性的本機值。

(繼承來源 DependencyObject)
ShowAt(DependencyObject, FlyoutShowOptions)

使用指定的選項顯示相對於指定專案放置的飛出視窗。

(繼承來源 FlyoutBase)
ShowAt(FrameworkElement)

顯示相對於指定專案放置的飛出視窗。

(繼承來源 FlyoutBase)
ShowAt(UIElement, Point)

顯示與指定之目標項目相關的指定位移所放置飛出視窗。

TryInvokeKeyboardAccelerator(ProcessKeyboardAcceleratorEventArgs)

嘗試叫用快捷鍵 (快捷鍵) 。

(繼承來源 FlyoutBase)
UnregisterPropertyChangedCallback(DependencyProperty, Int64)

取消先前透過呼叫 RegisterPropertyChangedCallback 註冊的變更通知。

(繼承來源 DependencyObject)

事件

Closed

發生於隱藏飛出視窗時。

(繼承來源 FlyoutBase)
Closing

發生於飛出視窗開始隱藏時。

(繼承來源 FlyoutBase)
Opened

發生於顯示飛出視窗時。

(繼承來源 FlyoutBase)
Opening

發生於顯示飛出視窗之前。

(繼承來源 FlyoutBase)

適用於

另請參閱