如果用户尚未移动滚动条,则自动滚动文本框

时间:2012-08-10 08:25:38

标签: wpf textbox autoscroll

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

textBox1.ScrollToEnd();

只要用户不移动ScrollBar,这就可以工作。

2 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:0)

我发现了一种不知怎的做法:

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的名称发生变化,这将停止工作。