gpt4 book ai didi

c# - 页面中心的 MenuFlyout

转载 作者:行者123 更新时间:2023-11-30 14:08:59 25 4
gpt4 key购买 nike

在我的 UWP 中,我试图在框架中心打开一个 MenuFlyout。

我怎样才能把它放在中心?

我试过这段代码,但它在框架顶部设置了弹出按钮。

private void ListView_ItemClick(object sender,ItemClickEventArgs e) {
var m = new MenuFlyout();
var t = new MenuFlyoutItem() { Text = "SomeTxt"};
m.Items.Add(t);
m.ShowAt((FrameworkElement)Frame);
}

甚至尝试添加 m.Placement = Windows.UI.Xaml.Controls.Primitives.FlyoutPlacementMode.Center;但 FlyoutPlacementMode 枚举不包含 Center .

最佳答案

不幸的是没有 Center FlyoutPlacementMode 中的值因此您需要编写更多代码来解决问题。

思路是先设置FlyoutPlacementModeFull , 然后我们改变 Style 而不是让内容拉伸(stretch)到填满整个页面的 MenuFlyoutPresenter使其内容水平和垂直居中。

所以我所做的是使用 Blend 生成以下默认样式并将其放置在 Page.Resources 中,然后我指定了 HorizontalAlignment="Center" VerticalAlignment="Center"在根上 Grid以使内容居中。

<Page.Resources>
<Style x:Key="CenteredMenuFlyoutPresenterStyle" TargetType="MenuFlyoutPresenter">
<Setter Property="Background" Value="{ThemeResource SystemControlBackgroundChromeMediumLowBrush}" />
<Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundChromeHighBrush}" />
<Setter Property="BorderThickness" Value="{ThemeResource FlyoutBorderThemeThickness}" />
<Setter Property="Padding" Value="{ThemeResource MenuFlyoutPresenterThemePadding}" />
<Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="ScrollViewer.VerticalScrollMode" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.IsHorizontalRailEnabled" Value="False" />
<Setter Property="ScrollViewer.IsVerticalRailEnabled" Value="False" />
<Setter Property="ScrollViewer.ZoomMode" Value="Disabled" />
<Setter Property="MinWidth" Value="{ThemeResource FlyoutThemeMinWidth}" />
<Setter Property="MaxWidth" Value="{ThemeResource FlyoutThemeMaxWidth}" />
<Setter Property="MinHeight" Value="{ThemeResource MenuFlyoutThemeMinHeight}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MenuFlyoutPresenter">
<Grid Background="{TemplateBinding Background}" HorizontalAlignment="Center" VerticalAlignment="Center">
<ScrollViewer x:Name="MenuFlyoutPresenterScrollViewer" AutomationProperties.AccessibilityView="Raw" HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" Margin="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" ZoomMode="{TemplateBinding ScrollViewer.ZoomMode}">
<ItemsPresenter />
</ScrollViewer>
<Border x:Name="MenuFlyoutPresenterBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

然后简单地将样式应用到 MenuFlyout像这样 -

var m = new MenuFlyout
{
Placement = FlyoutPlacementMode.Full,
MenuFlyoutPresenterStyle = (Style)this.Resources["CenteredMenuFlyoutPresenterStyle"]
};

var t = new MenuFlyoutItem() { Text = "SomeTxt" };
m.Items.Add(t);
m.ShowAt((FrameworkElement)sender);

关于c# - 页面中心的 MenuFlyout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32209809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com