Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get vertical scroll bars in an WPF TextBlock

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!

like image 724
Kristoffer Avatar asked Apr 11 '11 12:04

Kristoffer


People also ask

How do I add a vertical scrollbar in WPF?

How to enable scrollbar in a WPF TextBox. The simplest way to add scrolling functionality to a TextBox control is by enabling its horizontal and vertical scrolling. The HorizontalScrollBarVisibility and VerticalScrollBarVisibility properties are used to set horizontal and vertical scroll bars of a TextBox.

How do I add a vertical scrollbar?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

How do I add a vertical scroll bar in Visual Studio?

2. Double-click the form you want to use to display a scroll bar control. Drag and drop the control from the Visual Studio toolbox to the form. Use your mouse to place the scroll bar.

What is TextBlock WPF?

The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.


1 Answers

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>
like image 195
CodeNaked Avatar answered Oct 06 '22 17:10

CodeNaked