using System.Windows; using System.Windows.Controls; namespace EmbyToolbox.Behaviors; public static class TextBoxAutoScrollBehavior { public static readonly DependencyProperty AutoScrollToEndProperty = DependencyProperty.RegisterAttached( "AutoScrollToEnd", typeof(bool), typeof(TextBoxAutoScrollBehavior), new PropertyMetadata(false, OnAutoScrollToEndChanged)); public static bool GetAutoScrollToEnd(DependencyObject obj) { return (bool)obj.GetValue(AutoScrollToEndProperty); } public static void SetAutoScrollToEnd(DependencyObject obj, bool value) { obj.SetValue(AutoScrollToEndProperty, value); } private static void OnAutoScrollToEndChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not TextBox textBox) { return; } if ((bool)e.NewValue) { textBox.TextChanged += OnTextBoxTextChanged; textBox.ScrollToEnd(); } else { textBox.TextChanged -= OnTextBoxTextChanged; } } private static void OnTextBoxTextChanged(object sender, TextChangedEventArgs e) { if (sender is TextBox textBox) { textBox.ScrollToEnd(); } } }