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

JerryM 1,131 Reputation points
2021-05-17T05:18:33.85+00:00

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 ?Why ?
I have MS VS 2019 16.9.5, WPF .NET5.

97007-oblong.jpg

XAML code:

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

Here is complette source code:
https://drive.google.com/file/d/1XGg8jjMfqjBxcvYpy_2L9CsjuvzK0pXX/view?usp=sharing

Developer technologies | XAML
0 comments No comments
{count} votes

Accepted answer
  1. JerryM 1,131 Reputation points
    2021-05-18T06:57:15.577+00:00

    Wou I finally found the answer. Instead of this:

    Title="MainWindow" Width="800" Height="500" MaxWidth="800" MaxHeight="500">

    I have to use this:

    Title="MainWindow" SizeToContent="WidthAndHeight" WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen">

    and the most important thing is: SizeToContent="WidthAndHeight" ...

    yes ...

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. DaisyTian-1203 11,646 Reputation points
    2021-05-17T08:39:49.58+00:00

    @JerryM
    Please you check my answer for your another case. The total height of TitleBar and Grid in Window is greater than 400px. To check it, you could add <Grid Grid.Row="1" Grid.Column="0" Name="grid" Background="Azure"></Grid> to your Grid, and you will see my grid and your Rectangle beyond Window.The excess height is very close to the TitleBar
    97104-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.