TextBlock Right TextAligment Doesn't work inside a ScrollViewer

Miguel Cortés Rodríguez 21 Reputation points
2020-06-11T10:54:37.5+00:00

Hi,

Im coding in XAML. Im trying to create a TextBlock inside a ScrollViewer with a Right Textaligment. but no matter what I put, when the text exceeds the width of the TextBlock, It always shows the text from the left to the right. Here is a piece of code in XAML:

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" Style="{StaticResource EstiloScrollViewer}" Margin="0 5 0 0">
<TextBlock TextAlignment="Right" HorizontalAlignment="Right" Name="operacion" Foreground="#FFC6C7C7" FontSize="25" FontFamily="Segoe UI Light"
Text="99999999999999999999999998745" />
</ScrollViewer>

The result is:

9770-pic1.png

If I put the same Code without the ScrollViewer the result is the next:

<TextBlock TextAlignment="Right" HorizontalAlignment="Right" Name="operacion" Foreground="#FFC6C7C7" FontSize="25" FontFamily="Segoe UI Light" Text="99999999999999999999999998745" Margin="20 0"/>

9871-pic2.png

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points
    2020-06-12T07:18:54.29+00:00

    Add a SizeChanged event for your ScrollView to Scroll it to the right when it's size changed:

    private void ScrollViewer_SizeChanged(object sender, SizeChangedEventArgs e)
            {
                double d = myScrollViewer.ActualWidth;
                myScrollViewer.ScrollToHorizontalOffset(d*d);
            }
    

0 additional answers

Sort by: Most helpful