Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The answer provided by @BojinLi works well. After reading through the answer linked to by @GazTheDestroyer however, I decided to implement my own version for the TextBox, because it looked cleaner.</p> <p>To summarize, you can extend the behavior of the TextBox control by using an attached property. (Called ScrollOnTextChanged)</p> <p>Using it is simple:</p> <pre><code>&lt;TextBox src:TextBoxBehaviour.ScrollOnTextChanged="True" VerticalScrollBarVisibility="Auto" /&gt; </code></pre> <p>Here is the TextBoxBehaviour class:</p> <pre><code>using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; namespace MyNamespace { public class TextBoxBehaviour { static readonly Dictionary&lt;TextBox, Capture&gt; _associations = new Dictionary&lt;TextBox, Capture&gt;(); public static bool GetScrollOnTextChanged(DependencyObject dependencyObject) { return (bool)dependencyObject.GetValue(ScrollOnTextChangedProperty); } public static void SetScrollOnTextChanged(DependencyObject dependencyObject, bool value) { dependencyObject.SetValue(ScrollOnTextChangedProperty, value); } public static readonly DependencyProperty ScrollOnTextChangedProperty = DependencyProperty.RegisterAttached("ScrollOnTextChanged", typeof (bool), typeof (TextBoxBehaviour), new UIPropertyMetadata(false, OnScrollOnTextChanged)); static void OnScrollOnTextChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { var textBox = dependencyObject as TextBox; if (textBox == null) { return; } bool oldValue = (bool) e.OldValue, newValue = (bool) e.NewValue; if (newValue == oldValue) { return; } if (newValue) { textBox.Loaded += TextBoxLoaded; textBox.Unloaded += TextBoxUnloaded; } else { textBox.Loaded -= TextBoxLoaded; textBox.Unloaded -= TextBoxUnloaded; if (_associations.ContainsKey(textBox)) { _associations[textBox].Dispose(); } } } static void TextBoxUnloaded(object sender, RoutedEventArgs routedEventArgs) { var textBox = (TextBox) sender; _associations[textBox].Dispose(); textBox.Unloaded -= TextBoxUnloaded; } static void TextBoxLoaded(object sender, RoutedEventArgs routedEventArgs) { var textBox = (TextBox) sender; textBox.Loaded -= TextBoxLoaded; _associations[textBox] = new Capture(textBox); } class Capture : IDisposable { private TextBox TextBox { get; set; } public Capture(TextBox textBox) { TextBox = textBox; TextBox.TextChanged += OnTextBoxOnTextChanged; } private void OnTextBoxOnTextChanged(object sender, TextChangedEventArgs args) { TextBox.ScrollToEnd(); } public void Dispose() { TextBox.TextChanged -= OnTextBoxOnTextChanged; } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload