If you want the expanded text to automatically scroll with the newly added text only when the scroll bar has been dragged down, add the following class to your project
public class RichTextBoxThing : DependencyObject { public static bool GetIsAutoScroll(DependencyObject obj) { return (bool)obj.GetValue(IsAutoScrollProperty); } public static void SetIsAutoScroll(DependencyObject obj, bool value) { obj.SetValue(IsAutoScrollProperty, value); } public static readonly DependencyProperty IsAutoScrollProperty = DependencyProperty.RegisterAttached("IsAutoScroll", typeof(bool), typeof(RichTextBoxThing), new PropertyMetadata(false, new PropertyChangedCallback((s, e) => { RichTextBox richTextBox = s as RichTextBox; if (richTextBox != null) { if ((bool)e.NewValue) richTextBox.TextChanged += richTextBox_TextChanged; else if ((bool)e.OldValue) richTextBox.TextChanged -= richTextBox_TextChanged; } }))); static void richTextBox_TextChanged(object sender, TextChangedEventArgs e) { RichTextBox richTextBox = sender as RichTextBox; if ((richTextBox.VerticalOffset + richTextBox.ViewportHeight) == richTextBox.ExtentHeight || richTextBox.ExtentHeight < richTextBox.ViewportHeight) richTextBox.ScrollToEnd(); } }
then in any rich text field that you want the auto-scroll behavior to add the IsAutoSroll property
<RichTextBox ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" local:RichTextBoxThing.IsAutoScroll="True"/>
source share