In my scenario, the end user customizes his or her user interface by breaking it into rows and defining the height rule for those rows (fixed, fill space, fit content). I implement this using the WPF Grid.
The Grid starts filling the entire screen, and should not get any bigger - the user MUST be able to see the entire Grid at all times (scroll bars WITHIN rows are okay, but not scroll bars for the entire grid).
The crux of my problem: When the user creates one or more "auto" sized rows, the content in those rows can force the size of the entire grid to expand, introducing scroll bars, even when I've set the max height of the grid to a fixed number.
It gets worse when a star-sized row is involved, because once the grid is stretched a little, that star-sized row fills the available space, so the grid is PERMANENTLY stretched even when the auto-size row later shrinks.
I need to find a way to restrict "auto" rows so that they expand and shrink as needed, BUT the total actual height of all rows is never larger than the entire grid.
To illustrate, I have this grid with fixed max height, and rows representing all size modes.
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="200"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBlock>
abc<LineBreak/>
abc<LineBreak/>
abc<LineBreak/>
abc<LineBreak/>
abc<LineBreak/>
</TextBlock>
</ScrollViewer>
In this example, as the "abc" text block expands, the total height of the grid stretches beyond the fixed "300" maximum height. How can I prevent this behavior to guarantee the maximum height of the grid, while keeping the flexibility of the auto-size rows?
Okay, I discovered that I had to subclass Grid so that I could override Measure() and Arrange() layout steps.
I don't claim that this is a great general purpose solution, but it works for my scenario. Note especially that I'm not dealing with columns, since in my case, there's only one column. I'm also not positioning elements within the cells (I'm leaving them anchored at top-left).
If you need a more general solution, I think this is a very good start. The column problem is the same as the row problem, just in the other direction.
Here's a test case. Drop this in a Window and resize the window to see it working.