How can I make a RichTextBox scroll to the end when I add a new line? How can I make a RichTextBox scroll to the end when I add a new line? wpf wpf

How can I make a RichTextBox scroll to the end when I add a new line?


I had googled for your problem and found this post.In the section "Programming the RichTextBox" author had described about getting the behavior what you had been expecting.

Please check and let me know if it is of any use.


I tried to reproduce your problem and came up with the following solution

    <Window x:Class="CheckRichTextBox.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="170" Width="300">    <StackPanel>        <RichTextBox Height="100" Name="richTextBox1" IsReadOnly="True" VerticalScrollBarVisibility="Visible"/>        <Button Name="btnAdd" Content="Click me to add text" VerticalAlignment="Bottom" Click="BtnAddClick" />    </StackPanel></Window>

The code behind for the same is as below:

using System.Windows;namespace CheckRichTextBox{    /// <summary>    /// Interaction logic for MainWindow.xaml    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void BtnAddClick(object sender, RoutedEventArgs e)        {            richTextBox1.AppendText("You had Clicked the button for adding text\n");            richTextBox1.ScrollToEnd();        }    }}

This solves the problem of autoscroll, please check it and let me know if it is of any help.


I solved this problem using an Interactivity trigger and a very simple action.

The action looks like this:

public class ScrollToBottomAction : TriggerAction<RichTextBox>{    protected override void Invoke(object parameter)    {        AssociatedObject.ScrollToEnd();    }}

Then in my XAML I have this:

<RichTextBox IsReadOnly="True" VerticalScrollBarVisibility="Auto">     <i:Interaction.Triggers>            <i:EventTrigger EventName="TextChanged">                <interactivity:ScrollToBottomAction/>            </i:EventTrigger>     </i:Interaction.Triggers></RichTextBox>


I came up with the following solution for wpf richtextbox autoscroll

public partial class MainWindow{    private bool AutoScroll = true;    public MainWindow()    {        InitializeComponent();        yourRichTextBox.Loaded += (s, e) =>          {              var scrollViewer = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(yourRichTextBox, 0), 0) as ScrollViewer;              scrollViewer.ScrollChanged += (scroller, eScroller) => ScrollViewer_ScrollChanged(scroller, eScroller);          };    }    private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)    {        // User scroll event : set or unset autoscroll mode        if (e.Source as ScrollViewer != null && e.ExtentHeightChange == 0)        {   // Content unchanged : user scroll event            if ((e.Source as ScrollViewer).VerticalOffset == (e.Source as ScrollViewer).ScrollableHeight)            {   // Scroll bar is in bottom                // Set autoscroll mode                AutoScroll = true;            }            else            {   // Scroll bar isn't in bottom                // Unset autoscroll mode                AutoScroll = false;            }        }        // Content scroll event : autoscroll eventually        if (AutoScroll && e.ExtentHeightChange != 0 && e.Source as ScrollViewer != null)        {   // Content changed and autoscroll mode set            // Autoscroll            (e.Source as ScrollViewer).ScrollToVerticalOffset((e.Source as ScrollViewer).ExtentHeight);        }    }}