question

JerryM-2071 avatar image
0 Votes"
JerryM-2071 asked JerryM-2071 answered

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

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


dotnet-wpf-xaml
oblong.jpg (387.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JerryM-2071 avatar image
0 Votes"
JerryM-2071 answered


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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered

@JerryM-2071
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.


capture.png (19.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.