Reputation: 33
I have a Grid whose column size is set to *
to auto resize it upon window resize. I have a stack panel inside the grid. However I don't want it to resize when I add user controls to it, I just want a scrollbar to appear, I have tried the following but it doesn't seem to work:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="690"/>
<ColumnDefinition Width="Auto" MinWidth="170"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*" MinHeight="221"/>
</Grid.RowDefinitions>
<Grid Grid.Column="0"
Grid.Row="1"
Margin="10, 5, 10, 0">
<ScrollViewer HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible">
<StackPanel x:Name="ServersListPanel"/>
</ScrollViewer>
</Grid>
Upvotes: 0
Views: 43
Reputation: 216
Put the ScrollViewer in a DockPanel, remove the MinWidth and MinHeight constraints on the Grid, and set ScrollBarVisibility to "Auto":
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Column="0"
Grid.Row="1"
Margin="10, 5, 10, 0">
<DockPanel>
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
DockPanel.Dock="Top">
<StackPanel x:Name="ServersListPanel" />
</ScrollViewer>
</DockPanel>
</Grid>
</Grid>
The problem is because the ScrollViewer thinks it has infinite size because nothing is constraining its size. Setting the Width/Height of the ScrollViewer or one of its parent controls that contains the ScrollViewer would then constrain the size of your ScrollViewer and when its contained StackPanel needs more space to display its content, the content will be scrollable and the scrollbars will appear.
Upvotes: 0