System.Windows.Controls.Ribbon 顶部有一个大约 22px 的上边距(边框,space),为什么以及如何正确删除它?

System.Windows.Controls.Ribbon has a top margin (border, space) of around 22px on the top, why and how to remove it properly?

这就是问题所在(红色箭头所示的不必要的边距):

这是它的实际 XAML:

<Ribbon DockPanel.Dock="Top">

This is the patch(在我看来这是可行的,但不是真正的解决方案):

<Ribbon DockPanel.Dock="Top" Margin="0, -22, 0, 0">

有了补丁(对我来说更像是黑客):

为什么功能区顶部有边距 (border/space) 以及如何在不破解的情况下正确删除该边距(边距 -22 对我来说是破解)?

应用的解决方案(Ed Bayiates 解决方案):

<Ribbon DockPanel.Dock="Top" x:Name="MyRibbon" SizeChanged="RibbonSizeChanged">

private void RibbonSizeChanged(object sender, SizeChangedEventArgs e)
{
    ContentPresenter titlePanel = MyRibbon.Template.FindName("PART_TitleHost", MyRibbon) as ContentPresenter;
    if (titlePanel != null)
    {
        double titleHeight = titlePanel.ActualHeight;
        MyRibbon.Margin = new Thickness(MyRibbon.Margin.Left, -titleHeight, MyRibbon.Margin.Right, MyRibbon.Margin.Bottom);
    }
}

如果您使用功能区Window而不是标准 Window.

,我认为相关区域会折叠到 Window 标题栏中

如果您做不到,那么在该区域有三个项目占用相同的 22 像素 space。一个是 PART_TitleHost。第二个是没有 Name 属性的 DockPanel,第三个是没有 Name 属性的 Border。除非你 re-template 整个功能区我不认为你可以轻易摆脱这些。但是,如果将 y-margin 设置为该区域的确切大小,则可以使您的 hack 不那么棘手。在代码隐藏中,您可以获得标题栏的实际高度并重置功能区栏的边距:

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        ContentPresenter titlePanel = Ribbon.Template.FindName("PART_TitleHost", Ribbon) as ContentPresenter;
        if (titlePanel != null)
        {
            double titleHeight = titlePanel.ActualHeight;
            Ribbon.Margin = new Thickness(Ribbon.Margin.Left, -titleHeight, Ribbon.Margin.Right, Ribbon.Margin.Bottom);
        }
    }

图片没有那个代码:

图片那个代码: