是否可以让 WPF-TextBox 自动滚动但在移动 ScrollBar 时停止该行为。当用户滚动结束时,TextBox 应再次自动滚动以结束文本更改。
textBox1.ScrollToEnd();
只要用户不移动 ScrollBar,这将起作用。
是否可以让 WPF-TextBox 自动滚动但在移动 ScrollBar 时停止该行为。当用户滚动结束时,TextBox 应再次自动滚动以结束文本更改。
textBox1.ScrollToEnd();
只要用户不移动 ScrollBar,这将起作用。
我重新创建了我认为您的设置中的内容:
<StackPanel>
<ScrollViewer Height="50" x:Name="_scroll">
<TextBox x:Name="_text" Width="50" Text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." TextWrapping="Wrap"/>
</ScrollViewer>
<Button Click="Button_Click" Content="Add Text"/>
</StackPanel>
我假设您有一些事件在我的设置中编辑此文本框,它是一个按钮。及其点击处理程序:
private void Button_Click(object sender, RoutedEventArgs e)
{
var shouldScroll = _scroll.VerticalOffset == _scroll.ScrollableHeight;
_text.Text += "AAA ";
if (shouldScroll)
{
_scroll.ScrollToEnd();
}
}
仅当滚动条位于底部时,这将在文本编辑后自动滚动到结束。
编辑
对于更清洁的解决方案,您可以使用行为:
public class AutoScrollingBehavior : Behavior<ScrollViewer>
{
public object UpdateTrigger
{
get { return (object)GetValue(UpdateTriggerProperty); }
set { SetValue(UpdateTriggerProperty, value); }
}
public static readonly DependencyProperty UpdateTriggerProperty =
DependencyProperty.Register("UpdateTrigger", typeof(object), typeof(AutoScrollingBehavior), new UIPropertyMetadata(Update));
private bool IsScrolledDown
{
get { return (bool)GetValue(IsScrolledDownProperty); }
set { SetValue(IsScrolledDownProperty, value); }
}
public static readonly DependencyProperty IsScrolledDownProperty =
DependencyProperty.Register("IsScrolledDown", typeof(bool), typeof(AutoScrollingBehavior), new UIPropertyMetadata(false));
private static void Update(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((bool)d.GetValue(IsScrolledDownProperty))
{
var scroll = ((AutoScrollingBehavior)d).AssociatedObject;
scroll.ScrollToEnd();
}
}
protected override void OnAttached()
{
AssociatedObject.Loaded += new RoutedEventHandler(AssociatedObject_Loaded);
AssociatedObject.ScrollChanged += new ScrollChangedEventHandler(AssociatedObject_ScrollChanged);
}
private void AssociatedObject_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
IsScrolledDown = AssociatedObject.VerticalOffset == AssociatedObject.ScrollableHeight;
}
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
IsScrolledDown = AssociatedObject.VerticalOffset == AssociatedObject.ScrollableHeight;
}
}
和 xml:
<StackPanel>
<ScrollViewer Height="50" >
<e:Interaction.Behaviors>
<local:AutoScrollingBehavior UpdateTrigger="{Binding ElementName=_text,Path=Text}" />
</e:Interaction.Behaviors>
<TextBox x:Name="_text" Width="50" Text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." TextWrapping="Wrap"/>
</ScrollViewer>
<Button Click="Button_Click" Content="Add Text"/>
</StackPanel>
我找到了一种奇怪的方法来做到这一点:
public class AutoScrollTextBox : TextBox
{
ScrollViewer sv;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (Template != null)
{
sv = (ScrollViewer)Template.FindName("PART_ContentHost", this);
}
}
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
if (sv != null && sv.ScrollableHeight == sv.VerticalOffset)
{
this.ScrollToEnd();
}
}
}
但是,如果 TextBox-Template 中 ScrollViewer 的名称发生更改,这将停止工作。