Problem
In WPF, I have a TextBlock. I write a lot of lines to it, considerably more than its vertical height allows. When that happened, I expected a vertical scroll bar to appear immediately, but it didn’t. I looked in the Properties tab for a scroll bar property but couldn’t find one.
How can I make my TextBlock’s vertical scroll bar appear automatically when its contents exceed its height?
Clarification: Instead of writing directly to the XAML, I’d rather do it from the designer.
Asked by Bab Yogoo
Solution #1
Use a scroll viewer to wrap it up:
<ScrollViewer>
<TextBlock />
</ScrollViewer>
NOTE: As stated in the original question, this solution relates to a TextBlock (a read-only text element).
Use the ScrollViewer connected attributes to show scroll bars in a TextBox (an editable text element):
<TextBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto" />
Disabled, Auto, Hidden, and Visible are all valid values for these two properties.
Answered by Drew Noakes
Solution #2
can now use the following:
<TextBox Name="myTextBox"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="True">SOME TEXT
</TextBox>
Answered by vince
Solution #3
Something more appropriate would be:
<Grid Width="Your-specified-value" >
<ScrollViewer>
<TextBlock Width="Auto" TextWrapping="Wrap" />
</ScrollViewer>
</Grid>
This prevents the text in your textblock from overflowing and overlapping the items below it, which could happen if you don’t use the grid. Even though the textblock was already in a grid with other elements, this happened to me when I attempted different ways. Keep in mind that the textblock’s width should be Auto, and the required width should be specified in the Grid element. This is what I did in my code, and it works perfectly. HTH.
Answered by varagrawal
Solution #4
<ScrollViewer MaxHeight="50"
Width="Auto"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto">
<TextBlock Text="{Binding Path=}"
Style="{StaticResource TextStyle_Data}"
TextWrapping="Wrap" />
</ScrollViewer>
I’m handling it a another manner by using ScrollViewer’s MaxHeight property.
Simply change the MaxHeight value to display more or fewer lines of text. Easy.
Answered by Tony Wu
Solution #5
<ScrollViewer Height="239" VerticalScrollBarVisibility="Auto">
<TextBox AcceptsReturn="True" TextWrapping="Wrap" LineHeight="10" />
</ScrollViewer>
This is an example of how to use the scrolling TextBox in XAML as a text area.
Answered by John
Post is based on https://stackoverflow.com/questions/1192335/automatic-vertical-scroll-bar-in-wpf-textblock