通过 XAML 代码居中 WPF RibbonWindow 标题 [英] Center WPF RibbonWindow Title via XAML Code

查看:59
本文介绍了通过 XAML 代码居中 WPF RibbonWindow 标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 StackOverflow 上找到了一些关于我的问题的信息,所以我在我的窗口中引入了以下 XAML 代码.现在一切正常,而 WPF 窗口没有快速启动图标或上下文选项卡处于活动状态.

I've found some info on StackOverflow regarding my problem, so I introduced the following XAML code to my window. Now everything is fine, while the WPF window hasn't quick launch icons or contextual tabs active.

有没有办法通过 XAML 代码将应用程序标题完全居中.

Is there a way to center the application title completely via XAML Code.

<ribbon:Ribbon.TitleTemplate>
    <DataTemplate>
        <TextBlock TextAlignment="Center" HorizontalAlignment="Stretch"
            Width="{Binding ElementName=Window, Path=ActualWidth}">ApplicationTitle
            <TextBlock.Effect>
               <DropShadowEffect ShadowDepth="0" Color="MintCream " BlurRadius="10"/>   
            </TextBlock.Effect>
        </TextBlock>
    </DataTemplate>           
</ribbon:Ribbon.TitleTemplate>

推荐答案

这是一种非常天真的方法.它来自检查 RibbonWindow 及其伴随的 Ribbon 的可视化树.我已经用这个代码玩了几个小时(而且不再)——它的边缘有点粗糙,我不确定它是否完全没有错误.有一些优化要做,应该注意的是,我对 WPF 很烂;可能有更好的方法来做事.

Here's a very naïve way to do it. It comes about from inspecting the visual tree of a RibbonWindow and its concomitant Ribbon. I've been playing with this code for a couple of hours (and no longer) -- it's a bit rough around the edges and I'm not sure it's completely bug free. There's some optimizations to be made and it should be noted that I suck at WPF; there might be better way to do things.

代码的价值如下,但请先注意:

For what it's worth the code is below, but note first:

  • PART_Icon 模板的引用与您的问题没有直接关系,但与窗口的美感有关.

  • The references to the PART_Icon template are not directly related to your question, but it is related to the aesthetics of the window.

IsWin8OrHigherFindChild 的引用在我将在最后包含的类中.我对 Windows 8 的兴趣在于原生功能区库将标题文本居中,而早期版本的 Windows 则不然.我试图在这里效仿.

The references to IsWin8OrHigher and FindChild are in classes that I'll include at the end. My interest in Windows 8 is that the native ribbon library centres the title text, whereas earlier versions of Windows do not. I'm trying to emulate that here.

我不知道 RibbonWindow 在当前迭代中是如何随 Visual Studio 2012 一起提供的.Windows 8 上的渲染看起来很糟糕.毕竟,我很想用 TextBlock 重载 TitleTemplate 以摆脱默认发光并保留它.

I have no idea how the RibbonWindow was shipped with Visual Studio 2012 in its current iteration. The rendering on Windows 8 looks pretty miserable. After all this, I'm tempted to overload TitleTemplate with a TextBlock to get rid of the default glow and leave it at that.

RibbonWindow 在最大化后看起来不太好,是否自定义.

The RibbonWindow doesn't look very good maximized, customization or not.

当我开始编写这段代码时,这大概就是我的目标:

When I started writing this code, this was approximately what I was aiming for:

为了比较,这是 RibbonWindow 在没有自定义的情况下呈现自身的方式:

For comparison, this is how the RibbonWindow renders itself with no customisation:

这是使用 TitleTemplate 定义为 TextBlock 并带有 TextAlignment=Center" 但没有任何花哨的文本效果的渲染方式:

This is how it renders with TitleTemplate defined to a TextBlock with TextAlignment="Center" but otherwise without any fancy text effects:

通过下面的代码,我们得到了这个结果:

With the code below, we get this result:

主窗口:

public partial class MainWindow {
    public MainWindow() {
        InitializeComponent();
        if (Environment.OSVersion.IsWin8OrHigher()) {
            SizeChanged += (sender, args) => TitleHack();
            Activated += (sender, args) => TitleHack();
        }
    }

    public override void OnApplyTemplate() {
        base.OnApplyTemplate();
        if (!Environment.OSVersion.IsWin8OrHigher())
            return;
        var icon = GetTemplateChild("PART_Icon") as Image;
        if (icon == null)
            return;
        icon.Margin = new Thickness(icon.Margin.Left + 3, icon.Margin.Top + 2,
                                    icon.Margin.Right, icon.Margin.Bottom);
    }

    private void TitleHack() {
        var ribbonTitlePanel = MyRibbon.FindChild<FrameworkElement>("PART_TitlePanel");
        var qatTopHost = MyRibbon.FindChild<FrameworkElement>("QatTopHost");
        var titleHost = MyRibbon.FindChild<FrameworkElement>("PART_TitleHost");
        var tabGroup = MyRibbon.FindChild<FrameworkElement>("PART_ContextualTabGroupItemsControl");

        var qatTopHostLeft = qatTopHost.TransformToAncestor(ribbonTitlePanel).Transform(new Point(0, 0)).X;
        var tabGroupLeft = tabGroup.TransformToAncestor(ribbonTitlePanel).Transform(new Point(0, 0)).X;

        var width = ribbonTitlePanel.ActualWidth;

        if (tabGroup.Visibility == Visibility.Visible) {
            width -= tabGroup.ActualWidth;
            width -= tabGroupLeft - qatTopHostLeft;
        } else {
            width -= qatTopHost.ActualWidth;
        }

        if (ResizeMode != ResizeMode.NoResize && WindowStyle != WindowStyle.None)
            width -= 48; // For the min and max buttons

        titleHost.Width = width > 0 ? width : Double.NaN;
    }
}

OperatingSystemExtensionMethods.cs:

OperatingSystemExtensionMethods.cs:

public static class OperatingSystemExtensionMethods {
    private static readonly Version Windows8Version = new Version(6, 2);

    public static bool IsWin8OrHigher(this OperatingSystem that) {
        if (that.Platform != PlatformID.Win32NT)
            return false;
        return that.Version.CompareTo(Windows8Version) >= 0;
    }
}

DependencyObjectExtensionMethods.cs:

DependencyObjectExtensionMethods.cs:

public static class DependencyObjectExtensionMethods {
    public static T FindChild<T>(this DependencyObject that, string elementName)
        where T : FrameworkElement {
        var childrenCount = VisualTreeHelper.GetChildrenCount(that);

        for (var i = 0; i < childrenCount; i++) {
            var child = VisualTreeHelper.GetChild(that, i);
            var frameworkElement = child as FrameworkElement;

            if (frameworkElement != null && elementName == frameworkElement.Name)
                return (T) frameworkElement;

            if ((frameworkElement = frameworkElement.FindChild<T>(elementName)) != null)
                return (T) frameworkElement;
        }
        return null;
    }
}

这篇关于通过 XAML 代码居中 WPF RibbonWindow 标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆