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

如何消除C# UWP中按钮与其Flyout之间的间距?

消除Button与Flyout之间的边距

嘿,我来帮你搞定这个问题!要让Button和它的Flyout之间完全没有边距,咱们只需要调整MenuFlyout的展示容器样式,抵消掉系统默认的间距就行。直接修改你的代码如下:

<Grid>
    <Button Height="100" Width="100" Content="More" Name="MoreButton">
        <Button.Flyout>
            <MenuFlyout x:Name="MoreButtonFlyout" Placement="Bottom">
                <MenuFlyout.FlyoutPresenterStyle>
                    <Style TargetType="FlyoutPresenter">
                        <!-- 移除Flyout容器的默认边距和内边距 -->
                        <Setter Property="Margin" Value="0"/>
                        <Setter Property="Padding" Value="0"/>
                    </Style>
                </MenuFlyout.FlyoutPresenterStyle>
                <MenuFlyoutItem x:Name="ReplayOn" FontSize="12" FontFamily="Segoe UI Regular" HorizontalAlignment="Left" Foreground="#252525" Text="Item 1" Height="32"/>
            </MenuFlyout>
        </Button.Flyout>
    </Button>
</Grid>

关键细节说明:

  • 系统默认的FlyoutPresenter(承载Flyout内容的容器)自带了细微的边距,通过自定义FlyoutPresenterStyle并将Margin和Padding设为0,就能彻底消除Button与Flyout之间的间隙。
  • 如果调整后仍有极窄的缝隙,可以尝试将Margin设为-1,以此抵消系统层面的最小布局间距。

这样修改后,你的Flyout就会完全紧贴Button底部显示啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 16:47:42