Reputation: 1146
I have a StackPanel with a Grid inside of it. I want that Grid to always be the same size as the StackPanel, so I have the code setup as such:
<StackPanel Name="ContentPanel" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#FF2B2B2B" Margin="0,0,0,0">
<Grid HorizontalAlignment="Stretch" Margin="0, 0, 0, 0" VerticalAlignment="Top" Height="{Binding ActualHeight, ElementName=ContentPanel}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="5" />
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- Content Goes Here! -->
</Grid>
</StackPanel>
However, my Grid doesn't seem to be respecting the height. When I resize the Window with the Resize Handle, the grid expands correctly, but doesn't shrink correctly and retains the maximum dimensions. This means anything who's position is referenced via VerticalAlignment=Bottom is liable to 'disappear' from the screen if you resize it to be smaller.
To debug, I gave my StackPanel a set Height of 500 -- Looking at the design preview, I see this:
StackPanel Dimensions:
https://i.sstatic.net/POKwS.jpg
Grid Dimensions:
Even there, The Grid attempts to fill the space of the entire window, not just it's Parent StackPanel, which it should derive it's height from!
How do I force the Grid to inherent the height of the StackPanel? Alternatively, how do I always force the Grid to consume the entire StackPanel, even if a user resizes the Window?
I've tried changing VerticalAlignment values on all these items, and deleted all my content inside the grid, thinking it might be causing problems, but to no avail!
Upvotes: 0
Views: 88
Reputation: 310
The stack panel will adjust its height to match the height of its contents. Once the grid has reached a certain size (e.g. 800), the stack panel will not force the grid to become smaller.
One easy option to get what you want would be to put the StackPanel into a parent grid, and then bind the child grid to the parent grid's height.
Upvotes: 1