Anzurio
Anzurio

Reputation: 17014

Get the actual width and height of a MenuFlyout on UWP

Is there a way to get the actual width and height of a MenyFlyout in UWP?

I found that you can, for instance, set the MinWidth (very useful, by the way) like this:

 MenuFlyout m = this as MenuFlyout;
 Style s = new Windows.UI.Xaml.Style { TargetType = typeof(MenuFlyoutPresenter) };
 s.Setters.Add(new Setter(MenuFlyoutPresenter.MinWidthProperty, _minWidth + ""));
 m.MenuFlyoutPresenterStyle = s;

I just cannot find a way to simply query the width and height.

Upvotes: 0

Views: 637

Answers (1)

Jayden
Jayden

Reputation: 3286

It seems we can not get the actual width and height of a MenyFlyout in UWP.

You don't typically use a MenuFlyoutPresenter directly, either in XAML or code. Instead, you reference the MenuFlyoutPresenter type as the TargetType of the style you use for the MenuFlyout.MenuFlyoutPresenterStyle property.

For more info, see MenuFlyoutPresenter.

We can not use the MenuFlyoutPresenter.ActualWidth property to get the actual width.

The actual width of the MenuFlyout is depended on the MinWidth, MaxWidth of the MenuFlyoutPresenter and content's width of the MenuFlyout.

If you want to get the actual width and height to adapt to the page, you should be able to set the MinWidth and MaxWidth of MenuFlyoutPresenter. That no matter the content's width changes, the MenuFlyout can not be too large or too small for your page.

For example:

<MenuFlyout x:Name="MyMenuFlyout" >
    <MenuFlyout.MenuFlyoutPresenterStyle>
        <Style TargetType="MenuFlyoutPresenter">
            <Setter Property="MaxWidth" Value="500"/>
            <Setter Property="MinWidth" Value="50"/>
        </Style>
    </MenuFlyout.MenuFlyoutPresenterStyle>
</MenuFlyout>

Upvotes: 1

Related Questions