如果用户没有移动滚动条,则自动滚动文本框 [英] Autoscroll Textbox if user has not moved the scrollbar

查看:27
本文介绍了如果用户没有移动滚动条,则自动滚动文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以让 WPF-TextBox 自动滚动但在移动 ScrollBar 时停止该行为.当用户滚动到结束时,TextBox 应自动再次滚动以结束文本更改.

Is it possible to let a WPF-TextBox autoscroll but stop that behavior when the ScrollBar is moved. When the user scrolls to end the TextBox should scroll again automatically to end on text change.

textBox1.ScrollToEnd();

只要用户不移动滚动条,这就会起作用.

This would work as long as the user does not move the ScrollBar.

推荐答案

我重新创建了我认为您在设置中拥有的内容:

I have recreated what I think you have in your setup:

<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>

我假设您有一些事件可以在我的设置中编辑此文本框,它是一个按钮.以及它的点击处理程序:

I assume that you have some event that edits this textbox in my setup it is a button. And its click handler:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        var shouldScroll = _scroll.VerticalOffset == _scroll.ScrollableHeight;

        _text.Text += "AAA ";

        if (shouldScroll)
        {
            _scroll.ScrollToEnd();
        }
    }

仅当滚动条在底部时,这将在文本编辑后自动滚动到结尾.

This will auto scroll to end after text edit only if scrollbar is on the bottom.

编辑

对于更清洁的解决方案,您可以使用行为:

For cleaner solution you can use behavior:

  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;
    }
}

和xaml:

<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>

这篇关于如果用户没有移动滚动条,则自动滚动文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆