如何设置WPF窗口的宽度等于其标题栏的内容? [英] How to set WPF Window's width equal to the content in its Title Bar?

查看:772
本文介绍了如何设置WPF窗口的宽度等于其标题栏的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在WPF中有如下窗口:

If I have Window in WPF as follows:

<Window
    Title="Alter Window Width so that the complete title is shown."
    SizeToContent="WidthAndHeight"
    WindowStartupLocation="CenterOwner">

这窗口会自动调整,以确保所有的内容都是可见的。但它不会做的题一样的,所以它可能是当所示的窗口标题的一部分将被隐藏。

This window will automatically resize to make sure all of its content is visible. But it doesn't do the same for the title, so it's possible that a part of the title will be hidden when the window is shown.

什么可以做确保窗口的宽度足以显示在标题栏中的标题?

What can be done to make sure that the width of the window is enough to show the title in the title bar?

推荐答案

添加一个隐藏的文本块中窗口:

Add a hidden textblock in to the window:

<TextBlock 
   Text="{Binding Path=Title,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" 
   Visibility="Hidden" 
   Height="0" 
   Margin="100 0 0 0">
</TextBlock>

的保证金允许Windows控件被推出的方式。

The Margin allows for the windows controls to be pushed out of the way.

的高度使得控制不占用垂直空间。

The Height makes the control take up no vertical space.

的可见很可能不是必需的,因为零高度,而是将其设置为隐藏将导致它占用空间在画布上,但什么都不显示。

The Visiblity is probable not required because of the zero height, but by setting it to hidden will cause it to take up space on the canvas but show nothing.

这篇关于如何设置WPF窗口的宽度等于其标题栏的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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