You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改XAML Flyout尺寸?Flyout宽度计算异常求助

Fix Flyout Width Calculation Issue in UWP

I've run into exactly this problem before—when the window height is limited, the Flyout's width gets calculated based on the first ListView item instead of the longest one, leading to clipped content. The issue with your existing code is that when you set the MinWidth during initial opening events, the ListView hasn't finished laying out all its items yet, so ActualWidth still reflects the width of the first item. By the second opening, the layout is cached, so the value becomes correct.

Here are a few reliable solutions to fix this:

1. Disable Horizontal Scrolling on the ListView (Simplest XAML-Only Fix)

By disabling horizontal scrolling, the ListView will automatically expand its width to fit the longest item. This forces the Flyout to adopt that full width right from the first opening:

<AppBarButton>
    <AppBarButton.Flyout>
        <Flyout>
            <ListView ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                <x:String>Short</x:String>
                <x:String>Very very very long</x:String>
                <x:String>Short</x:String>
                <x:String>Short</x:String>
                <x:String>Short</x:String>
                <x:String>Short</x:String>
                <x:String>Short</x:String>
                <x:String>Short</x:String>
            </ListView>
        </Flyout>
    </AppBarButton.Flyout>
</AppBarButton>

This works because the ListView can't scroll horizontally anymore, so it has to size itself to accommodate the widest content. The Flyout will then match this calculated width automatically.

2. Force Layout Update + Dispatcher Delay in Code

If you need to keep horizontal scrolling enabled, you can force the ListView to finish layout before setting the Flyout's MinWidth. Using Dispatcher.RunAsync ensures we wait until the UI thread has finished rendering all ListView items:

void YourPage::OnFlyoutOpening(Platform::Object^ sender, Windows::UI::Xaml::Controls::Primitives::FlyoutBaseOpeningEventArgs^ e)
{
    auto flyout = dynamic_cast<Flyout^>(sender);
    auto listView = dynamic_cast<ListView^>(flyout->Content);
    
    // Force the ListView to complete layout immediately
    listView->UpdateLayout();
    
    // Wait for the next UI cycle to ensure ActualWidth is accurate
    auto dispatcher = Windows::UI::Xaml::Window::Current->Dispatcher;
    dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([this, listView]()
    {
        auto presenterStyle = ref new Windows::UI::Xaml::Style(FlyoutPresenter::typeid);
        presenterStyle->Setters->Append(ref new Setter(FlyoutPresenter::MinWidthProperty, listView->ActualWidth));
        myFlyout->FlyoutPresenterStyle = presenterStyle;
    }));
}

Don't forget to hook up the Opening event to your handler in XAML:

<Flyout x:Name="myFlyout" Opening="OnFlyoutOpening">

3. Bind FlyoutPresenter's MinWidth to ListView's ActualWidth

You can also use a XAML binding to link the FlyoutPresenter's MinWidth directly to the ListView's ActualWidth. To ensure it works on the first opening, combine it with the UpdateLayout() call in the Opening event:

<AppBarButton>
    <AppBarButton.Flyout>
        <Flyout x:Name="myFlyout" Opening="OnFlyoutOpening">
            <Flyout.FlyoutPresenterStyle>
                <Style TargetType="FlyoutPresenter">
                    <Setter Property="MinWidth" Value="{Binding ElementName=filesListView, Path=ActualWidth}" />
                </Style>
            </Flyout.FlyoutPresenterStyle>
            <ListView x:Name="filesListView">
                <!-- Your items here -->
            </ListView>
        </Flyout>
    </AppBarButton.Flyout>
</AppBarButton>

The UpdateLayout() call in the event handler will make sure the binding picks up the correct width value right when the Flyout starts opening.


内容的提问来源于stack exchange,提问作者Andrey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 00:57:37