TextBlock with vertical scrollbar

I have a TextBlock that can contain long text, so I want to add a vertical scrollbar to it. My initial attempt was to wrap a ScrollViewer around it. This works, but the problem is that when I zoom in, the width also increases. I tried disabling the horizontal scrollbar as follows:

<ScrollViewer IsTabStop="True" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">

But this did not solve the problem. I also tried to snap width:

Width="{Binding ElementName=Scroller, Path=ViewportWidth}"

It didnโ€™t help either.

So my question is: how do I add a vertical scrollbar to it, but have a fixed width and wrapped text for the TextBlock inside? Here is my full code:

 <ScrollViewer Grid.Row="1" IsTabStop="True" VerticalScrollBarVisibility="Auto"> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" TextWrapping="Wrap" TextAlignment="Center"/> </ScrollViewer> 
+4
source share
2 answers

There are two parts to this answer: the first is just to use a TextBox :

 <TextBox ScrollViewer.VerticalScrollBarVisibility="Visible" Text="Something really really really really really really really really really long" Style="{StaticResource TextBlockStyle}" /> 

The second part is just a Style TextBox to make it look like a TextBlock :

 <Style x:Key="TextBlockStyle" TargetType="{x:Type TextBox}"> <Setter Property="Background" Value="{x:Null}" /> <Setter Property="BorderBrush" Value="{x:Null}" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="Padding" Value="0" /> <Setter Property="IsReadOnly" Value="True" /> <Setter Property="IsTabStop" Value="False" /> <Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="TextWrapping" Value="Wrap" /> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Background" Value="{x:Null}" /> </Trigger> </Style.Triggers> </Style> 

Feel free to remove any of these properties if they are not appropriate for your situation.

+4
source
  <TextBox HorizontalAlignment="Center" VerticalAlignment="Top" TextWrapping="Wrap" TextAlignment="Center" VerticalScrollBarVisibility="Auto" Width="300" Style="{StaticResource TextBlockStyle}"/> 

You do not need a ScrollViewer wrapped in a TextBox , the TextBox control has its own ScrollViewer . And you need to determine the width of the TextBox so that the scrollbar knows its fixed width and wraps the text.

Then you need to style the TextBox to look like a TextBlock

Good reason this ScrollViewer will not work according to Ifeanyi Echeruo from Microsoft, MSDN

ScrollViewer first asks its content how much it would like to have no restrictions if the content needs more space than the viewer now has time to strike at some ScrollBars

If there are no restrictions, TextBlock will always choose a return size, where all text is suitable for one line.

ScrollViewer with ScrollBars will never receive a TextBlock for wrapping.

However, you can come up with a Measure \ Arrange combination for your panel, which is almost similar to ScrollViewer, but I canโ€™t think of any logic that can satisfy both restrictions without explicit knowledge of the behavior of these children

+1
source

Source: https://habr.com/ru/post/1501661/


All Articles