Modify MenuFlyoutPresenter to align with button in UWP

I am trying to get MenuFlyout to align to the bottom left corner of a button. I can achieve this if I put the button to the left of the screen, but if the button is anywhere else the MenuFlyout will always center right below the button. I suspect I need to create a template and change a property there, but I don't know what I need to change to achieve this.

I pasted my xaml below with a note on where I suspect I need to make changes. I am new to xaml, any help or guidance would be much appreciated. Thank!

<Page
    x:Class="ButtonMenuFlyout.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ButtonMenuFlyout"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <Style x:Key="BasicTextStyle" TargetType="MenuFlyoutItem">
            <Setter Property="FontFamily" Value="Segoe UI" />
            <Setter Property="FontSize" Value="13" />
            <Setter Property="HorizontalAlignment" Value="Right" />
            <Setter Property="Padding" Value="0,0,0,0" />
            <Setter Property="MinHeight" Value="36" />
        </Style>
        <Style x:Key="MenuFlyoutPresenterStyle1" TargetType="MenuFlyoutPresenter">
            <Setter Property="Background" Value="#FFFFFF" />
            <Setter Property="HorizontalAlignment" Value="Right" />
            <Setter Property="Padding" Value="20,0,20,16" />
            <Setter Property="Visibility" Value="Visible"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="MenuFlyoutPresenter">
                        <Grid Background="{TemplateBinding Background}">
                            <!--[change property in here]-->
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

        </Style>
    </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button x:Name="buttonMenuFlyout" 
                    Content="..." 
                    VerticalAlignment="Top"  
                    HorizontalAlignment="right"
                    Padding="16,4"
                    Margin="120,10,0,5"
                    Background="White">
            <Button.Flyout>
                <MenuFlyout  Opened="MenuFlyout_Opened" MenuFlyoutPresenterStyle="{StaticResource MenuFlyoutPresenterStyle1}">
                    <MenuFlyoutItem Style="{StaticResource BasicTextStyle}" Text="Settings" Click="MenuFlyoutItem_Click"  />
                    <MenuFlyoutItem Style="{StaticResource BasicTextStyle}" Text="Feedback" Click="MenuFlyoutItem_Click_1"/>
                    <MenuFlyoutItem Style="{StaticResource BasicTextStyle}" Text="Notebook" Click="MenuFlyoutItem_Click_2" />
                </MenuFlyout>
            </Button.Flyout>
        </Button>
    </Grid>
</Page>

      

+3


source to share


1 answer


In your method, MenuFlyout_Opened

you can explicitly call the method ShowAt(UIElement, Point)

. This first parameter is likely to be yours buttonMenuFlyout

; the second is your offset, which can be roughly equal new Point(0, buttonMenuFlyout.ActualHeight)

.



0


source







All Articles