Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set WPF Window's width equal to the content in its Title Bar?

Tags:

c#

.net

wpf

xaml

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?

like image 972
teenup Avatar asked Apr 24 '15 05:04

teenup


People also ask

How do you make a WPF window resizable?

To make the window resizable you could add invisible (Fill=”Transparent”) rectangles, one for each side of the window and another one for each corner of the window, to our XAML defined control template as described in the above blog post. Hope that helps.

How do I change the title of a window in WPF?

How do I programmatically change the Title in a wpf window? Change the Title from "Contacts" to "Something new" when the program finds new information as it starts. You should use binding. Bind the title property of window to a property of your DataContext.

What is the difference between a page and a window in WPF when you are adding a new file in the Solution Explorer?

Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications.

How do I change the color of the title bar in WPF?

You can change the background colour of the TilteBar by overriding the RibbonWindow template in App. Xaml and change the value of Background property in TitleBarStyleKey. The following code example demonstrates the same.


1 Answers

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>

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.

like image 61
benPearce Avatar answered Oct 09 '22 05:10

benPearce