【问题标题】:Get the actual width and height of a MenuFlyout on UWP获取 UWP 上 MenuFlyout 的实际宽度和高度
【发布时间】:2017-08-23 20:35:37
【问题描述】:

有没有办法在 UWP 中获取 MenyFlyout 的实际宽度和高度?

我发现你可以像这样设置MinWidth(顺便说一句,非常有用):

 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;

我只是找不到简单查询宽度和高度的方法。

【问题讨论】:

  • 您应该在浮出控件的 content 控件中定义高度和宽度。
  • 我从未使用过弹出窗口,但应该有办法获取它的 FrameworkElement 并订阅在 ActualWidth 或 ActualHeight 更改时触发的 SizeChanged 事件。

标签: uwp


【解决方案1】:

我们似乎无法在 UWP 中获取 MenyFlyout 的实际宽度和高度。

您通常不会在 XAML 或代码中直接使用 MenuFlyoutPresenter。相反,您将 MenuFlyoutPresenter 类型引用为您用于 MenuFlyout.MenuFlyoutPresenterStyle 属性的样式的 TargetType。

有关详细信息,请参阅MenuFlyoutPresenter

我们不能使用 MenuFlyoutPresenter.ActualWidth 属性来获取实际宽度。

MenuFlyout的实际宽度取决于MenuFlyoutPresenterMinWidthMaxWidthMenuFlyout的内容宽度。

如果你想得到实际的宽度和高度来适应页面,你应该可以设置MenuFlyoutPresenterMinWidthMaxWidth。无论内容的宽度如何变化,MenuFlyout 对于您的页面来说都不能太大或太小。

例如:

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

【讨论】:

    猜你喜欢
    • 2013-09-24
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 2011-07-13
    相关资源
    最近更新 更多