How to set WPF window width equal to contents in title bar?

If I have a window in WPF, follow these steps:

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

This window will automatically change to make sure all of its contents are visible. But he does not do the same for the name, so it is possible that part of the name will be hidden when the window is displayed.

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

+4
source share
1 answer

Add a hidden text block to the window:

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

Margin allows you to exclude Windows controls.

Height makes the control not occupying vertical space.

- , , , , .

+6

Source: https://habr.com/ru/post/1584414/


All Articles