An oblong 100x300 in a Grid cell (400x300) is too large. Why ?

JerryM

Member
Joined
May 13, 2021
Messages
10
Programming Experience
Beginner
Hi,
I tried to create a Grid with two columns and two rows. A left bottom Cell (0,1) has size 400x300px - see the image below. I tried to insert an oblong of size 100x300 into the cell, but the oblong is too large. :) I do not understand why ? The Height of the cell is 300, the Height of the oblong is also 300, but the oblong overlaps the cell of approx 20 pixels. Why ? Why ? Why ?
I have MS VS 2019 16.9.5, WPF .NET5.

oblong.jpg


XAML code:

C#:
<Window x:Class="WpfApp.MainWindow"
        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:WpfApp"
        mc:Ignorable="d"
        Title="MainWindow"  Width="600px" Height="400px">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="400px"/>
            <ColumnDefinition Width="200px"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="100px"/>
            <RowDefinition Height="300px"/>
        </Grid.RowDefinitions>
        
        <Rectangle Grid.Column="0" Grid.Row="1"
                   Width="100px"
                   Height="300px"
                   HorizontalAlignment="Left"
                   VerticalAlignment="Top"                   
                   Margin="200px,0px,0px,0px"
                   Stroke="Black" />

    </Grid>
</Window>
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,749
Location
Chesapeake, VA
Programming Experience
10+
Your main window height on line 8 is not accounting for the size of the window chrome.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,749
Location
Chesapeake, VA
Programming Experience
10+
The size give to the main window is the window client and non-client area combined. Windows has been like this since Windows 1.0.
 

JerryM

Member
Joined
May 13, 2021
Messages
10
Programming Experience
Beginner
wou I finally found the correct answer:

SizeToContent="WidthAndHeight"

and whole XAML:

C#:
<Window Name="wnd_001" x:Class="WpfApp.MainWindow"
        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:WpfApp"
        mc:Ignorable="d"
        Title="MainWindow" SizeToContent="WidthAndHeight" WindowStyle="ToolWindow"  WindowStartupLocation="CenterScreen">
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,749
Location
Chesapeake, VA
Programming Experience
10+
How can that be the correct answer when that was not the question you were asking? The question you were asking was why was the oblong (sic) going past the bounds of the client area of the window. Your "answer" was instead of answering why was to to just force WPF to recompute the window size based on the contents instead.
 
Top Bottom