locked
Scroll Viewer hiding textbox and not auto scrolling to the end RRS feed

  • Question

  • Hi, I have an issue with the scroll viewer not scrolling to the end of the line automatically. Hence, the last line of my textbox is hidden and the user cannot see it unless they scroll down.

    This is inconvenient for my user since they have to scroll down every time they hit the return key.

    I thought I had found a solution to this in code for the TextBox SizeChanged event by using the ScrollToVerticalOffset method of the ScrollViewer as follows:

    private void NoteContentTextBox_SizeChanged(object sender, SizeChangedEventArgs e)
    {
    	textScroller.ScrollToVerticalOffset(NoteContentTextBox.ActualHeight);
    }

    This works fine if the user enters new lines at the end of the textbox. But if the user tries to delete a line from the middle of the textbox it jumps to the end of the textbox.

    Is there any way I can somehow detect if the user is at the last line of the textbox and then only call the ScrollToVerticalOffset method?

    Here is my XAML for my content block if there is a solution to this using XAML, but I can't find any.

     <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,-30,0,60" Height="Auto">
                <ScrollViewer Height="Auto" Canvas.ZIndex="4" x:Name="textScroller">
                <Grid Margin="0,5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*"/>
                        <ColumnDefinition Width="10*"/>
                    </Grid.ColumnDefinitions>
    
                    <Grid Background="{StaticResource PhoneAccentBrush}" 
                          Grid.Column="0" 
                          Height="Auto"
                          Canvas.ZIndex="3">
                        <TextBlock
                               x:Name="NoteBookBar"
                               Text="NOTEBOOK NAME"
                               FontFamily="Segoe WP Light"
                               FontSize="20"
                               FontWeight="Bold"
                               VerticalAlignment="Center"
                               HorizontalAlignment="Center"
                               TextAlignment="Center"
                               Height="Auto"
                               Width="600"
                               TextWrapping="NoWrap"
                               RenderTransformOrigin="0.5,0.5"
                               Margin="-200"
                               UseLayoutRounding="False">
                            <TextBlock.RenderTransform>
                                <CompositeTransform Rotation="-90"/>
                            </TextBlock.RenderTransform>
                        </TextBlock>
                    </Grid>
    
                    <Grid Grid.Column="1" Height="Auto">
                        <TextBox x:Name="NoteContentTextBox" 
                             Margin="-8,-10,10,-10"
                             Height="Auto"
                             Background="{StaticResource PhoneForegroundBrush}"
                             AcceptsReturn="True"
                             TextWrapping="Wrap"
                             TextChanged="NoteContentTextBlock_TextChanged"
                             FontFamily="Segoe UI Semibold" 
                             VerticalScrollBarVisibility="Visible"
                             HorizontalScrollBarVisibility="Disabled"
                             SizeChanged="NoteContentTextBox_SizeChanged"/>
                    </Grid>
                </Grid>
                </ScrollViewer>
            </Grid>


    Monday, May 5, 2014 10:15 PM

Answers

  • This isn't perfect but works in most situations to keep the cursor visible:

    private void NoteContentTextBox_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        textScroller.ScrollToVerticalOffset(textScroller.VerticalOffset +
            e.NewSize.Height - e.PreviousSize.Height);
    }


    Richard Woo
    Tuesday, May 6, 2014 4:33 AM

All replies

  • This isn't perfect but works in most situations to keep the cursor visible:

    private void NoteContentTextBox_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        textScroller.ScrollToVerticalOffset(textScroller.VerticalOffset +
            e.NewSize.Height - e.PreviousSize.Height);
    }


    Richard Woo
    Tuesday, May 6, 2014 4:33 AM
  • Thanks alot. That works just as I wanted it to.
    Tuesday, May 6, 2014 7:58 PM