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.
VerticalAlignment="Stretch" ), but on the small screen, automatically display the vertical scroll bar when needed.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With