Search code examples
c#wpfxamlscrolltextblock

Unable to scroll in WPF textblocks using ScrollViewer


I have 2 textblocks on my form. I need to have vertical scrollbars in each of them. Due to some reason, I am not able to get the scrollbars on both. Kindly provide me with some idea.

    <Grid>
    <Button Content="COMPARE" HorizontalAlignment="Left" Margin="216,30,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    <TextBox x:Name="TextBox1" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,69,0,0" TextWrapping="Wrap" RenderTransformOrigin="-1.351,-2.164" Height="242" Width="226" Loaded="TextBox1_Loaded" />
    <ScrollViewer>
    <TextBlock x:Name="TextBlock1" HorizontalAlignment="Left" Margin="10,69,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="-1.351,-2.164" Height="242" Width="226" Loaded="TextBlock1_Loaded" />
    </ScrollViewer>
        <TextBox x:Name="TextBox2" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalAlignment="Left" Margin="258,69,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="-1.351,-2.164" Height="242" Width="226" Loaded="TextBox2_Loaded"/>
    <ScrollViewer>
    <TextBlock x:Name="TextBlock2" HorizontalAlignment="Left" Margin="258,69,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="-1.351,-2.164" Height="242" Width="226" Loaded="TextBlock2_Loaded_1"/>
    </ScrollViewer>
    <Button Content="EDIT" HorizontalAlignment="Left" Margin="409,30,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
    <Button Content="HOME" HorizontalAlignment="Left" Margin="21,26,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.23,0.986" Click="Button_Click_2"/>
</Grid>

Solution

  • Try to check this out: 1. XAML Code:

        <ScrollViewer Grid.Column="0" Grid.Row="0">
            <TextBlock  x:Name="ATextBlock" ></TextBlock></ScrollViewer>
        <ScrollViewer Grid.Row="0" Grid.Column="1" >
            <TextBlock x:Name="BtTextBlock" ></TextBlock></ScrollViewer>
    

    the problem was; the Height definition on the TextBlock. Just remove it and that is.

    regards.,