Using WPF, I have a Window, that is filled with one Grid. Currently this Grid has one Row and three Columns, each cell filled with the same UserControl. Future versions might add more Rows to the Grid.
I want to make my window have the size of the Grid.
<Window
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns: ...
...
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="600"
Title="MainWindow" Height="600" Width="600">
<Grid Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<View:ImageView Grid.Row="0" Grid.Column="0" DataContext="..."/>
<View:ImageView Grid.Row="0" Grid.Column="1" DataContext="..."/>
<View:ImageView Grid.Row="0" Grid.Column="2" DataContext="..."/>
</Grid>
The Window has a Width of 600. The Grid will stretch to this window. It will have a Width about 600. The Grid will have three columns with the same Width, something near 200. The ImageView in each cell will stretch to fill the Cell. Each Cell will have a Width about 200.
This determines the Height of the Cells, and thus the Height of the Grid.
Addition: after comments I don't want to resize the window. After it decided about what it should displays and what sizes the controls should have to display it nicely, there is no need to resize it.
For debugging, to see what happens, I resized the mainwindow, and recorded what happens to the various Height / Width / ActualHeight / ActualWidth values.
I noticed that the Grid is resized, such that its Width exactly fills the Width of the Window. Resizing will also Resize the cells and the ImageViews that are in the Cells.
This fitting is not the case with the Height. I can make the Height window Higher and Lower than the Height of the Grid.
I want to set the Height of the window such that it is exactly around the Grid.
I expected something like:
<Window ... Width = "600" Height = "auto" />
Nope, the height is way large than it should be, somewhere near 1000. The Grid is still about 300 x 600.
<Window ... Width = "600" Height = "*" />
This leads to an exception.
Maybe Binding? Something like this?
<Window ... Width = "600" Height = "{Binding Height, ElementName=MainGrid, Mode=OneWay}"/>
<Window ... Width = "600" Height = "{Binding ActualHeight, ElementName=MainGrid, Mode=OneWay}"/>
Nope, still way to large.
So, how to make sure that the window is exactly around the Grid?