Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic vertical scroll bar in WPF TextBlock?

I have a TextBlock in WPF. I write many lines to it, far exceeding its vertical height. I expected a vertical scroll bar to appear automatically when that happens, but it didn't. I tried to look for a scroll bar property in the Properties pane, but could not find one.

How can I make vertical scroll bar created automatically for my TextBlock once its contents exceed its height?

Clarification: I would rather do it from the designer and not by directly writing to the XAML.

like image 870
Bab Yogoo Avatar asked Jul 28 '09 07:07

Bab Yogoo


People also ask

How do I add a vertical ScrollBar in WPF?

VerticalAlignment="Stretch" ), but on the small screen, automatically display the vertical scroll bar when needed.

What is the difference between TextBox and TextBlock in WPF?

Text inside a TextBlock can not be made selectable by the user. TextBoxes are used for displaying text more focused for content input or when content is needed to be made selectable by the user. The TextBox can only be set to one colour, one font size, one font type etc.

How do I add a ScrollBar to XAML?

The <ScrollBar> element of XAML represents a scroll bar control in UI. The ScrollBar control is used to add horizontal and vertical scroll bars to content controls. This tutorial and code examples show how to use a ScrollBar control in a WPF app using XAML. The following code example creates a ScrollBar.


1 Answers

Wrap it in a scroll viewer:

<ScrollViewer>     <TextBlock /> </ScrollViewer> 

NOTE this answer applies to a TextBlock (a read-only text element) as asked for in the original question.

If you want to show scroll bars in a TextBox (an editable text element) then use the ScrollViewer attached properties:

<TextBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"          ScrollViewer.VerticalScrollBarVisibility="Auto" /> 

Valid values for these two properties are Disabled, Auto, Hidden and Visible.

like image 101
Drew Noakes Avatar answered Sep 20 '22 14:09

Drew Noakes