通过 XAML 代码居中 WPF RibbonWindow 标题
我在 StackOverflow
上找到了一些有关我的问题的信息,因此我在窗口中引入了以下 XAML
代码。 现在一切都很好,但 WPF
窗口没有快速启动图标或上下文选项卡处于活动状态。
有没有办法通过 XAML 代码将应用程序标题完全居中。
<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>
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.
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种非常幼稚的方法。它来自于检查
RibbonWindow
及其伴随的Ribbon
的可视化树。我已经使用这段代码几个小时了(现在已经不再了)——它的边缘有点粗糙,我不确定它是否完全没有错误。需要进行一些优化,并且应该指出的是,我对 WPF 很烂;可能有更好的方法来做事。对于其价值,代码如下,但首先请注意:
对
PART_Icon
模板的引用与您的问题没有直接关系,但它与窗口的美观有关。对
IsWin8OrHigher
和FindChild
的引用位于我将在最后包含的类中。我对 Windows 8 的兴趣在于,本机功能区库将标题文本居中,而早期版本的 Windows 则不然。我试图在这里模仿这一点。我不知道
RibbonWindow
是如何在当前版本的 Visual Studio 2012 中附带的。 Windows 8 上的渲染看起来非常糟糕。毕竟,我很想用TextBlock
重载TitleTemplate
来摆脱默认发光并保留它。RibbonWindow
最大化后看起来不太好,无论是否自定义。当我开始编写这段代码时,这大约是我的目标:
作为比较,这就是如何
RibbonWindow
无需自定义即可呈现自身:这是它的呈现方式
TitleTemplate
定义为带有TextAlignment="Center"
的TextBlock
,但没有任何花哨的文本效果:使用下面的代码,我们得到以下结果:
MainWindow:
OperatingSystemExtensionMethods.cs:
DependencyObjectExtensionMethods.cs:
Here's a very naïve way to do it. It comes about from inspecting the visual tree of a
RibbonWindow
and its concomitantRibbon
. 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:
The references to the
PART_Icon
template are not directly related to your question, but it is related to the aesthetics of the window.The references to
IsWin8OrHigher
andFindChild
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.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 overloadTitleTemplate
with aTextBlock
to get rid of the default glow and leave it at that.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:
For comparison, this is how the
RibbonWindow
renders itself with no customisation:This is how it renders with
TitleTemplate
defined to aTextBlock
withTextAlignment="Center"
but otherwise without any fancy text effects:With the code below, we get this result:
MainWindow:
OperatingSystemExtensionMethods.cs:
DependencyObjectExtensionMethods.cs:
那应该工作得很好。我刚刚对其进行了测试,标题按其应有的方式居中。
That should be working fine. I've just tested it and the title centers as it should.
如果你想让它真正居中,它需要是:
if you want it truly centered, it needs to be: