菜单弹出在页面的中心
本文关键字:菜单 | 更新日期: 2023-09-27 18:09:43
在我的UWP中,我试图在框架中心打开一个菜单窗口。
我怎样才能把它放在中间?
我试过这段代码,但是它在框架顶部设置了飞出。
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 enum不包含Center
。
不幸的是,在FlyoutPlacementMode
中没有Center
值,所以您需要编写更多的代码来解决这个问题。
我们的想法是首先将FlyoutPlacementMode
设置为Full
,然后我们改变MenuFlyoutPresenter
的Style
,使其内容在水平和垂直方向上居中,而不是让内容伸展以填充整个页面。
所以我所做的是使用Blend生成以下默认样式,并将其放置在Page.Resources
中,然后我在根Grid
上指定HorizontalAlignment="Center" VerticalAlignment="Center"
,以便将内容居中。
<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);