Search code examples
c#wpfwindow

How to remove the extra vertical blank space from a WPF window


How can I remove the blank space?

<Window x:Class="AnnoCopyDialog.RebarPasteWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:AnnoCopyDialog"
        mc:Ignorable="d"
        ResizeMode="NoResize"
        Background="#f0f0f0"
        FontSize="11"
        Title="Paste Window"  Width="420" Height="Auto">
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Margin="0 15 0 0" HorizontalAlignment="Right">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Content="OK" HorizontalAlignment="Center" Width="70"/>
            </StackPanel>
        </Grid>
    </Grid>
</Window>

enter image description here


Solution

  • Use SizeToContent="Height"

    <Window x:Class="AnnoCopyDialog.RebarPasteWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:AnnoCopyDialog"
            mc:Ignorable="d"
            ResizeMode="NoResize"
            Background="#f0f0f0"
            FontSize="11"
            Title="Paste Window"  Width="420" Height="Auto" SizeToContent="Height">
        <Grid Margin="10">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" Margin="0 15 0 0" HorizontalAlignment="Right">
    
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                    <Button Content="OK" HorizontalAlignment="Center" Width="70"/>
                </StackPanel>
    
    
            </Grid>
        </Grid>
    </Window>