Andy Clarke
Andy Clarke

Reputation: 3262

Window set to size around content leaves gap on each side

Can someone please tell me why, even though I've set my Window to SizeToContent and my content is Width=40 does the app load bigger across the Width?

The following example sizes to height, but the width is about 100 pixels (estimation).

Thanks

<Window x:Class="WpfApplication2.RetailButs"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="RetailButs" SizeToContent="WidthAndHeight"
        WindowStyle="None" MinWidth="0" >
        <Grid x:Name="LayoutRooty" Width="40" Height="40">
            <Border x:Name="LayoutRootx" Background="White" BorderBrush="Black" CornerRadius="8" BorderThickness="4" >
            </Border>
        </Grid>
</Window>

Upvotes: 1

Views: 834

Answers (2)

brunnerh
brunnerh

Reputation: 184506

There was a similar question recently which i was unable to find. The size is that which would be needed if the WindowStyle were the default 3D border with all the buttons which take up a lot of space. I think there is some way to update the layout, maybe you can figure something out.

Edit: Set the window's Width="0" (or any other value for that matter) and it should update correctly. The size to content will override the value.

(If you set the WindowStyle to ToolWindow it always fits correctly)

Upvotes: 0

NoWar
NoWar

Reputation: 37633

The extra size you have got comes from 3 top right window buttons.

So the best solution is just take buttons out (1) and put some minimum size for the window (2).

Try this and it will be exactly what you need.

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"  Width="40" Height="40" SizeToContent="WidthAndHeight" WindowStyle="None" ResizeMode="NoResize">
    <Grid x:Name="LayoutRooty" Width="40" Height="40">

        <Border x:Name="LayoutRootx" Background="White" BorderBrush="Black" CornerRadius="8" BorderThickness="4" >

        </Border>

    </Grid>
</Window>

Upvotes: 1

Related Questions