user3683303
user3683303

Reputation: 51

Layout of window content is incorrect in case of Window.SizeToContent = SizeToContent.WidthAndHeight

I made a simple example. There is a tool window. It has Grid as the content with three areas. The first area is TextBox with some text, the second one is Label and the third is another TextBox. The heights of Grid's rows are respectively: "*", "30", "*". For the window I set SizeToContent = SizeToContent.WidthAndHeight. When I open it The height of the first TextBox is bigger then the second one. When I try to reseize the window the heights become proportional as expected.

There is xaml of the window:

 <Window
    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" 
    mc:Ignorable="d" 
    x:Class="WpfApplication5.MainWindow"
    Title="MainWindow" MinHeight="200" MinWidth="200"
    DataContext="System.Data.DataSet" Width="291" d:DesignHeight="246"
    WindowStyle="ToolWindow"
    SizeToContent="WidthAndHeight">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
            <TextBox x:Name="Text1" />
        <Label Grid.Row="1" Content="SOME TEXT"/>
            <TextBox x:Name="Text2" Grid.Row="2"/>
    </Grid>
</Window>

And code behind:

using System.Data;
using System.Windows;

namespace WpfApplication5
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            Text1.Text =
                "some text\nsome text\nsome text\nsome text\nsome text\nsome text\nsome text\nsome text\nsome text\n";
        }
    }
}

My question is: How to fix these jumping heights? Initially the heights should be proportional.

Initial Image :

enter image description here

After Resizing (what I want on load) :

enter image description here

Upvotes: 3

Views: 821

Answers (2)

Rachel
Rachel

Reputation: 132648

It looks like SizeToContent is applied after the initial grid is rendered. So the grid is initially calculated with with both textboxes of an equal size, then SizeToContent gets applied and resizes all controls to their minimum size.

As a way around it, you can apply a SharedSizeGroup to the Grid Rows so they are forced to be of an equal size when SizeToContent tries to resize everything.

 <Window ...
    WindowStyle="ToolWindow"
    SizeToContent="WidthAndHeight">
    <Grid Grid.IsSharedSizeScope="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"  SharedSizeGroup="A"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"  SharedSizeGroup="A"/>
        </Grid.RowDefinitions>
            <TextBox x:Name="Text1" />
        <Label Grid.Row="1" Content="SOME TEXT"/>
            <TextBox x:Name="Text2" Grid.Row="2"/>
    </Grid>
</Window>

Then on initial load, it looks like this :

enter image description here

Upvotes: 1

Vie
Vie

Reputation: 863

Your sample works as it should work. Size is calculating from childs to root. So your TextBox has content with big height, then Grid makes first and third rows equal, then Window.SizeToContent = "WidthAndHeight" fit/minimize all sizes to size of their content. Thus you see windows without free spaces.

When you trigger windows resize, the rule SizeToContent become false, because you manually set windows size and it could be recalculated. So Grids rows become equal size.

To check it you can:

  1. remove SizeToContent="WidthAndHeight" and you will see grid with equal rows height as you expected
  2. or add e.g. KeyDown event like

    private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
    {
        SizeToContent = SizeToContent.WidthAndHeight;
    }
    

    So at the beginning Grid rows will have different size, after resizing, they become equal, BUT if you press any key, window immediately minimize his size and Grid rows will be not equal.

Upvotes: 1

Related Questions