WPF sizes of controls ..

JerryM 1,121 Reputation points
2021-05-16T17:20:07.97+00:00

Hi,
can someone explain me the size definition of WPF controls ?
I have a grid. Cell 0,1 has size 300x400. If I put a rectangle into the cell, the maximum height of the rectangle is 383 pixels only, but the width of grid cell is 400. Where is problem ??? Am I imbecile ?

96994-geometry.jpg

<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="800" Height="500"  MaxWidth="800" MaxHeight="500">  
    <Grid>  
        <Grid.ColumnDefinitions>  
            <ColumnDefinition Width="500" />  
            <ColumnDefinition Width="300" MaxWidth="300"/>  
        </Grid.ColumnDefinitions>  
  
        <Grid.RowDefinitions>  
            <RowDefinition Height="100" />  
            <RowDefinition Height="400" MaxHeight="400"/>  
        </Grid.RowDefinitions>  
  
        <DockPanel Grid.Column="0" Grid.Row="0">  
            <Menu DockPanel.Dock="Top">  
                <MenuItem Header="_File" FontSize="24" FontFamily="Arial">  
                    <MenuItem Header="_New" >  
                        <MenuItem.Icon>  
                            <Image Source="c:\1\WPF-01\Button-Previous-icon.png" />  
                        </MenuItem.Icon>  
                    </MenuItem>  
                    <MenuItem Header="_Open" />  
                    <MenuItem Header="_Save" />  
                    <Separator />  
                    <MenuItem Header="_Exit" />  
                </MenuItem>  
            </Menu>  
        </DockPanel>  
  
        <Button  Grid.Column="0" x:Name="btn001" Content="Hello" Width="200" Height="200" Click="btn001_Click"  Grid.Row="1" />  
  
        <TabControl Name="tabCtrl_001"  
                    Grid.Column="1" Grid.Row="1"  
                    Width="300"   
                    MaxWidth="300"   
                    Height="383"   
                    MaxHeight="400"  
                    VerticalAlignment="Top"  
                    Margin="0,0,0,0"  
                    Padding="0,0">  
  
            <TabItem Name="tabItem_001"  
                     Header="TabItem"   
                     Padding="0,0"   
                     Margin="0,0,0,0"  
                       
                     >  
  
                <Grid   
                      Name="grid_002"  
                      Background="#FFE5E5E5"   
                      Margin="0,0,0,0"   
                      VerticalAlignment="Stretch"  
                      HorizontalAlignment="Stretch"  
                        
                      >  
  
                    <Grid.ColumnDefinitions>  
                        <ColumnDefinition Width="300" />  
                    </Grid.ColumnDefinitions>  
                    <Grid.RowDefinitions>  
                        <RowDefinition Height="400" />  
                    </Grid.RowDefinitions>  
  
                    <WrapPanel >  
                        <ScrollViewer Loaded="scrlView_001_Loaded"  
                                  Name="scrlView_001"   
                                  Grid.Column="0" Grid.Row="0"  
                                  HorizontalAlignment="Center"  
                                  VerticalAlignment="Top"  
                                  VerticalScrollBarVisibility="Visible"   
                                  HorizontalScrollBarVisibility="Visible"  
                                  Padding="0,0"   
                                  Width="300"  
                                  Height="312"  
                                  BorderBrush="Green" BorderThickness="10"  
                                  >  
                            <DockPanel Name="stackPanel1"   
                                    Width="310"   
                                    Height="800"  
                                    MaxWidth="310">  
                                <GroupBox HorizontalAlignment="Left"   
                                      VerticalAlignment="Top"  
                                      Header="Text"   
                                      Margin="4,4,0,0"   
                                      Padding="0,0"  
                                      Height="401"   
                                      Width="180"   
                                      Background="White" >  
                                </GroupBox>  
                            </DockPanel>  
  
                        </ScrollViewer>  
                    </WrapPanel>  
  
                </Grid>  
            </TabItem>  
            <TabItem Header="TabItem">  
                <Grid Background="#FFE5E5E5"/>  
            </TabItem>  
        </TabControl>  
        <Rectangle HorizontalAlignment="Left" Margin="426,0,0,0"   
                   Stroke="Black" Width="39"   
                   Height="400" VerticalAlignment="Center" Grid.Row="1"/>  
  
  
    </Grid>  
</Window>  
  
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
786 questions
{count} votes

Accepted answer
  1. JerryM 1,121 Reputation points
    2021-05-18T06:54:45.54+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" ...

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2021-05-17T05:58:40.997+00:00

    Your Window's height is 500, and your Grid total height is 500, but your Window also has a TitleBar. The total height of TitleBar and Grid in Window is greater than 500.
    A standard window is shown in the following figure:
    96949-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