Why is this WPF window independent of its contents having SizeToContent = "WidthAndHeight"?

I created this simple demo because I'm having problems with automatic sizing in WPF. This is simple code:

<Window x:Class="UnderstandSizing.Window3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" SizeToContent="WidthAndHeight" ResizeMode="NoResize" > <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <TabControl Grid.Row="0" > <TabItem Header="CCCC" /> </TabControl> <Button Grid.Row="1" Content="Text" /> </Grid> </Window> 

Since the grid is set to "Auto" and the window is set to sizetocontent, I expected that I would have a TabControl and a button, and there is nothing in the window, but there are empty spaces on it. Take a look:

Design time Development time

Runtime lead time

Something is missing me ...

+4
source share
1 answer

It looks like the window has a minimum width that you cannot lower below, even if you resize. This may be related to the buttons at the top. If you set WindowStyle to ToolWindow , it will be displayed as the correct size. Interestingly, if you set it to None , it will initially be the same size as the default, but you can resize it smaller.

In any case, do you really want your window to be so small? If your width is greater, the window will fit snugly into the control.

+4
source

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


All Articles