Why this WPF window is not sizing to its content having SizeToContent=“WidthAndHeight”?

StackOverflow https://stackoverflow.com/questions/9099659

Вопрос

I've created this simple demo because I'm having some trouble making auto-sizing work in WPF. This is the 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>

As the grid has everything set to Auto and the window is configured to sizetocontent I expected to have the TabControl and the button and nothing else at the window but there are empty spaces on it. Look:

Design time Design-time

Runtime Run-time

I must be missing something ...

Это было полезно?

Решение

It appears the the window has a minimum width that you can't go below even if you turn resize on. This might have to do with the buttons at the top. If you set WindowStyle to ToolWindow it will appear 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.

Anyway, do you really want your window to be this small? If your width is bigger, the window will correctly snugly fit the control.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top