do you know why my scrollviewer isn't working? (WPF)
<Grid Grid.Row="1" Grid.Column="1" Width="350">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1" Margin="10,0,0,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0">
<Run Text="{Binding SelectedItem.Name}" FontFamily="/Fonts/#Cascadia Mono" FontWeight="SemiBold" FontSize="18"/>
<Run Text=" "/>
<Run Text="{Binding SelectedItem.Version}" Foreground="Gray" FontFamily="Open Sans" FontSize="16"/>
</TextBlock>
<Button x:Name="installButton" Grid.Column="1" Content="{Binding InstallButtonHeader}"
Visibility="Collapsed" Margin="10,-5,0,-10" Command="{Binding InstallCommand}"/>
</Grid>
<TextBlock Text="{Binding SelectedItem.Author}" FontSize="12" Foreground="Gray" FontFamily="Open Sans"/>
</StackPanel>
<ScrollViewer Grid.Row="2" Margin="10" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled">
<TextBlock Text="{Binding SelectedItem.LongDescription}" TextWrapping="Wrap"/>
</ScrollViewer>
</Grid>
It worked here without problems:
<Grid Grid.Row="1" Grid.Column="1" Width="350">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1" Margin="10,0,0,0">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding SelectedItem.Name}" FontFamily="/Fonts/#Cascadia Mono" FontWeight="SemiBold" FontSize="18"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding SelectedItem.Version}" Foreground="Gray" FontFamily="Open Sans" FontSize="16" VerticalAlignment="Center"/>
</StackPanel>
<TextBlock Text="{Binding SelectedItem.Author}" FontSize="12" Foreground="Gray" FontFamily="Open Sans"/>
</StackPanel>
<Button x:Name="installButton" Grid.Row="2" Content="{Binding InstallButtonHeader}" Visibility="Collapsed" Margin="10,10,0,0" Command="{Binding InstallCommand}"/>
<ScrollViewer Grid.Row="3" Margin="10" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<TextBlock Text="{Binding SelectedItem.LongDescription}" TextWrapping="Wrap"/>
</ScrollViewer>
</Grid>
Not working one had auto height enabled, I thought that this was the problem, but after removing it, error was still occuring.
In case of the
ScrollViewerit always needs a size constraint.Using a row height of
*means the element inside the row can take the remaining available size. This is results in a concrete height value (what ever the remaining height of the available height may be).A height of
Automeans the element inside the row can have any size it desires - even if it means it would exceed the available space (stretch to infinity).Now UI elements have different sizing behaviors. Some like the
StackPanelstrive to occupy the least possible space while a control like theScrollViewerwill always take as much space as possible (it's greedy). This means, if theScrollVieweris inside a container that doesn't put a size constraint on it (for example by setting the row height of a parentGridtoAuto) it will stretch to occupy an infinite size.Because everything will fit into an infinite area, the child elements of the
ScrollViewerwill now always fit. Now that everything always fits the available area, the scrollable area becomes0: because the scroll bar's purpose is to allow to bring elements that doesn't fit the available space into view. If everything fits than there is nothing to scroll into view.To solve this problem the
ScrollViewermust have a size constraint. Either implicitly imposed by the parent container e.g., by setting row height of aGridto a value other thanAuto, or by given theScrollVieweran explicitHeightandWidth.