我有一个 WPF 应用程序,其中包含一个多行文本框,用于显示调试文本输出。
如何设置 TextBox 以便将文本附加到框中时,它会自动滚动到文本框的底部?
- 我正在使用 MVVM 模式。
- 理想情况下,纯 XAML 方法会很好。
- TextBox 本身不一定是焦点。
@BojinLi 提供的答案效果很好。然而,在阅读了@GazTheDestroyer 链接的答案后,我决定为 TextBox 实现我自己的版本,因为它看起来更干净。
总而言之,您可以通过使用附加属性来扩展 TextBox 控件的行为。(称为 ScrollOnTextChanged)
使用它很简单:
<TextBox src:TextBoxBehaviour.ScrollOnTextChanged="True" VerticalScrollBarVisibility="Auto" />
这是 TextBoxBehaviour 类:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace MyNamespace
{
public class TextBoxBehaviour
{
static readonly Dictionary<TextBox, Capture> _associations = new Dictionary<TextBox, Capture>();
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;
}
}
}
}
此解决方案的灵感来自Scott Ferguson 的附加属性解决方案,但避免存储内部关联字典,因此代码更短:
using System;
using System.Windows;
using System.Windows.Controls;
namespace AttachedPropertyTest
{
public static class TextBoxUtilities
{
public static readonly DependencyProperty AlwaysScrollToEndProperty = DependencyProperty.RegisterAttached("AlwaysScrollToEnd",
typeof(bool),
typeof(TextBoxUtilities),
new PropertyMetadata(false, AlwaysScrollToEndChanged));
private static void AlwaysScrollToEndChanged(object sender, DependencyPropertyChangedEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb != null) {
bool alwaysScrollToEnd = (e.NewValue != null) && (bool)e.NewValue;
if (alwaysScrollToEnd) {
tb.ScrollToEnd();
tb.TextChanged += TextChanged;
} else {
tb.TextChanged -= TextChanged;
}
} else {
throw new InvalidOperationException("The attached AlwaysScrollToEnd property can only be applied to TextBox instances.");
}
}
public static bool GetAlwaysScrollToEnd(TextBox textBox)
{
if (textBox == null) {
throw new ArgumentNullException("textBox");
}
return (bool)textBox.GetValue(AlwaysScrollToEndProperty);
}
public static void SetAlwaysScrollToEnd(TextBox textBox, bool alwaysScrollToEnd)
{
if (textBox == null) {
throw new ArgumentNullException("textBox");
}
textBox.SetValue(AlwaysScrollToEndProperty, alwaysScrollToEnd);
}
private static void TextChanged(object sender, TextChangedEventArgs e)
{
((TextBox)sender).ScrollToEnd();
}
}
}
据我所知,它的行为完全符合预期。这是一个测试用例,窗口中有几个文本框,允许AlwaysScrollToEnd
以各种方式设置附加属性(硬编码、CheckBox.IsChecked
绑定和代码隐藏):
xml:
<Window x:Class="AttachedPropertyTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AttachedPropertyTest" Height="800" Width="300"
xmlns:local="clr-namespace:AttachedPropertyTest">
<Window.Resources>
<Style x:Key="MultiLineTB" TargetType="TextBox">
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Height" Value="60"/>
<Setter Property="Text" Value="{Binding Text, ElementName=tbMaster}"/>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBox Background="LightYellow" Name="tbMaster" Height="150" AcceptsReturn="True"/>
<TextBox Style="{StaticResource MultiLineTB}" Grid.Row="1" local:TextBoxUtilities.AlwaysScrollToEnd="True"/>
<TextBox Style="{StaticResource MultiLineTB}" Grid.Row="2"/>
<TextBox Style="{StaticResource MultiLineTB}" Grid.Row="3" Name="tb3" local:TextBoxUtilities.AlwaysScrollToEnd="True"/>
<TextBox Style="{StaticResource MultiLineTB}" Grid.Row="4" Name="tb4"/>
<CheckBox Grid.Column="1" Grid.Row="4" IsChecked="{Binding (local:TextBoxUtilities.AlwaysScrollToEnd), Mode=TwoWay, ElementName=tb4}"/>
<Button Grid.Row="5" Click="Button_Click"/>
</Grid>
</Window>
代码隐藏:
using System;
using System.Windows;
using System.Windows.Controls;
namespace AttachedPropertyTest
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
void Button_Click(object sender, RoutedEventArgs e)
{
TextBoxUtilities.SetAlwaysScrollToEnd(tb3, true);
}
}
}
嗯,这似乎是一件有趣的事情,所以我尝试了一下。从一些凝视来看,似乎没有一种直接的方法可以“告诉”文本框将自身滚动到最后。所以我换了个思路。WPF 中的所有框架控件都有一个默认的 Style/ControlTemplate,从 Textbox 控件的外观判断,其中必须有一个 ScrollViewer 来处理滚动。那么,为什么不直接使用默认文本框 ControlTemplate 的本地副本并以编程方式获取 ScrollViewer。然后我可以告诉 ScrollViewer 将其内容滚动到最后。事实证明这个想法有效。
这是我编写的测试程序,可以使用一些重构,但您可以通过查看它来了解它:
这是 XAML:
<Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication3="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!--The default Style for the Framework Textbox-->
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
<SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
<SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />
<ControlTemplate x:Key="MyTextBoxTemplate" TargetType="{x:Type TextBoxBase}">
<Border x:Name="Border" CornerRadius="2" Padding="2" Background="{StaticResource WindowBackgroundBrush}"
BorderBrush="{StaticResource SolidBorderBrush}" BorderThickness="1">
<ScrollViewer Margin="0" x:Name="PART_ContentHost" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}" />
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="MyTextBox" TargetType="{x:Type TextBoxBase}">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="MinWidth" Value="120" />
<Setter Property="MinHeight" Value="20" />
<Setter Property="AllowDrop" Value="true" />
<Setter Property="Template" Value="{StaticResource MyTextBoxTemplate}"></Setter>
</Style>
</Window.Resources>
<Grid>
<WpfApplication3:AutoScrollTextBox x:Name="textbox" TextWrapping="Wrap" Style="{StaticResource MyTextBox}"
VerticalScrollBarVisibility="Visible" AcceptsReturn="True" Width="100" Height="100">test</WpfApplication3:AutoScrollTextBox>
</Grid>
</Window>
以及背后的代码:
using System;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
textbox.AppendText("Line " + i + Environment.NewLine);
}
}
}
public class AutoScrollTextBox : TextBox
{
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
// Make sure the Template is in the Visual Tree:
// http://stackoverflow.com/questions/2285491/wpf-findname-returns-null-when-it-should-not
ApplyTemplate();
var template = (ControlTemplate) FindResource("MyTextBoxTemplate");
var scrollViewer = template.FindName("PART_ContentHost", this) as ScrollViewer;
//SelectionStart = Text.Length;
scrollViewer.ScrollToEnd();
}
}
}
一种更便携的方法可能是使用附加属性,例如在listbox 的类似问题中。
(属性变化VerticalOffset
时才设置)Text
与其他答案类似,但没有静态事件和控制字典。(恕我直言,如果可能,最好避免静态事件)。
public class ScrollToEndBehavior
{
public static readonly DependencyProperty OnTextChangedProperty =
DependencyProperty.RegisterAttached(
"OnTextChanged",
typeof(bool),
typeof(ScrollToEndBehavior),
new UIPropertyMetadata(false, OnTextChanged)
);
public static bool GetOnTextChanged(DependencyObject dependencyObject)
{
return (bool)dependencyObject.GetValue(OnTextChangedProperty);
}
public static void SetOnTextChanged(DependencyObject dependencyObject, bool value)
{
dependencyObject.SetValue(OnTextChangedProperty, value);
}
private static void OnTextChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var textBox = dependencyObject as TextBox;
var newValue = (bool)e.NewValue;
if (textBox == null || (bool)e.OldValue == newValue)
{
return;
}
TextChangedEventHandler handler = (object sender, TextChangedEventArgs args) =>
((TextBox)sender).ScrollToEnd();
if (newValue)
{
textBox.TextChanged += handler;
}
else
{
textBox.TextChanged -= handler;
}
}
}
这只是其他已发布解决方案的替代方案,这些解决方案是我在寻找一段时间后发现的最好的解决方案之一(即简洁和 mvvm)。
“ScrollToEnd”方法的问题是 TextBox 必须是可见的,否则它不会滚动。
因此,更好的方法是将 TextBox Selection 属性设置为文档结尾:
static void tb_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb == null)
{
return;
}
// set selection to end of document
tb.SelectionStart = int.MaxValue;
tb.SelectionLength = 0;
}
顺便说一句,第一个示例中的内存泄漏处理可能是不必要的。TextBox 是发布者,静态附加属性事件处理程序是订阅者。发布者保留对订阅者的引用,这可以使订阅者保持活动状态(而不是相反。)因此,如果 TextBox 超出范围,对静态事件处理程序的引用也会如此(即,没有内存泄漏。)
所以连接附加属性可以更简单地处理:
static void OnAutoTextScrollChanged
(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
TextBox tb = obj as TextBox;
if (tb == null)
{
return;
}
bool b = (bool)args.NewValue;
if (b)
{
tb.TextChanged += tb_TextChanged;
}
else
{
tb.TextChanged -= tb_TextChanged;
}
}