Wpf – Unable to get vertical scroll bars in an WPF TextBlock

scrollbarstextblockwpf

I'm presenting text in a wpf TextBlock control (.Net 3.5). The content of the textblock varies depending on what the user selects in a list box. The text wraps, so I don't need an horizontal scroll bar. However, there is often more text than the amount the window can display, so I need a vertical scroll bar.

As I started searching I quickly found that the answer is to wrap the TextBlock in a ScrollViewer. However, It Does Not Work (TM) and I'm hoping someone can help me work out why.

This is the structure of the UI code:

<Window x:Class=..>
    <StackPanel>
        <ListBox HorizontalAlignment="Stretch"
                 VerticalAlignment="Top"  Height="200"
                 SelectionChanged="listbox_changed" SelectionMode="Single">
        </ListBox>
        <Button Click="Select_clicked">Select</Button>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <TextBlock Name="textblock" TextWrapping="Wrap"/>
        </ScrollViewer>
    </StackPanel>
</Window>

When the user selects an item in the list box, some text associated with this item is presented in the TextBlock. I would have thought that the code as it stands should have been all that's required, but it never provides me with a scroll bar.

Searching and experimenting have given me two clues: the root of the problem might be related to me updating the content of the TextBlock dynamically, and that the TextBlock does not resize itself based on the new content. I found a posting that seemed relevant that said that by setting the Height of the TextBlock to its ActualHeight (after having changed its content), it would work. But it didn't (I can see no effect of this).

Second, if I set the height (during design time) of the ScrollViewer, then I do get a vertical scroll bar. For instance, if I set it to 300 in the xaml above, the result is almost good in that the window as first opened contains a TextBlock with a vertical scroll bar when (and only when) I need it. But if I make the window larger (resizing it with the mouse during runtime), the ScrollViewer does not exploit the new window size and instead keeps its height as per the xaml which of course won't do.

Hopefully, I've just overlooked something obvious..

Thanks!

Best Answer

Because your ScrollViewer is in a StackPanel it will be given as much vertical space as it needs to display it's content.

You would need to use a parent panel that restricts the vertical space, like DockPanel or Grid.

<DockPanel>
    <ListBox DockPanel.Dock="Top" HorizontalAlignment="Stretch"
             VerticalAlignment="Top"  Height="200"
             SelectionChanged="listbox_changed" SelectionMode="Single">
    </ListBox>
    <Button DockPanel.Dock="Top" Click="Select_clicked">Select</Button>
    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <TextBlock Name="textblock" TextWrapping="Wrap"/>
    </ScrollViewer>
</DockPanel>
Related Topic