Search code examples
wpfxamllayoutwindow

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>

Solution

  • 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>