question

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

WPF sizes of controls ..

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>


dotnet-wpf-xaml
geometry.jpg (246.7 KiB)
· 1
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" ...

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

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.


capture.png (18.0 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.