C# 添加新行时,如何使 RichTextBox 滚动到末尾?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10308475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:17:06  来源:igfitidea点击:

How can I make a RichTextBox scroll to the end when I add a new line?

c#wpfxamlrichtextbox

提问by Merad

I have several read only RichTextBox's that are used for logging output. Since they're read only they don't seem to automatically scroll when the text is updated. I canuse the TextChanged event to force a scroll to end, but is there not simply a way to set a property or something in the XAML so that scrolling happens like normal?

我有几个只读的 RichTextBox 用于记录输出。由于它们是只读的,因此它们在文本更新时似乎不会自动滚动。我可以使用 TextChanged 事件来强制滚动结束,但是否有一种简单的方法可以在 XAML 中设置属性或其他内容,以便像平常一样进行滚动?

采纳答案by Pank

I had googled for your problem and found thispost. In the section "Programming the RichTextBox" author had described about getting the behavior what you had been expecting.

我在谷歌上搜索了你的问题并找到了这篇文章。在“对 RichTextBox 编程”一节中,作者描述了如何获得您所期望的行为。

Please check and let me know if it is of any use.

请检查并让我知道它是否有任何用处。



I tried to reproduce your problem and came up with the following solution

我试图重现您的问题并提出以下解决方案

    <Window x:Class="CheckRichTextBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="170" Width="300">
    <StackPanel>
        <RichTextBox Height="100" Name="richTextBox1" IsReadOnly="True" VerticalScrollBarVisibility="Visible"/>
        <Button Name="btnAdd" Content="Click me to add text" VerticalAlignment="Bottom" Click="BtnAddClick" />
    </StackPanel>
</Window>

The code behind for the same is as below:

后面的代码如下:

using System.Windows;

namespace CheckRichTextBox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void BtnAddClick(object sender, RoutedEventArgs e)
        {
            richTextBox1.AppendText("You had Clicked the button for adding text\n");
            richTextBox1.ScrollToEnd();
        }
    }
}

This solves the problem of autoscroll, please check it and let me know if it is of any help.

这解决了自动滚动的问题,请检查它并让我知道它是否有任何帮助。

回答by Samuel Hyman

I solved this problem using an Interactivitytrigger and a very simple action.

我使用交互触发器和一个非常简单的操作解决了这个问题。

The action looks like this:

操作如下所示:

public class ScrollToBottomAction : TriggerAction<RichTextBox>
{
    protected override void Invoke(object parameter)
    {
        AssociatedObject.ScrollToEnd();
    }
}

Then in my XAML I have this:

然后在我的 XAML 中,我有这个:

<RichTextBox IsReadOnly="True" VerticalScrollBarVisibility="Auto">
     <i:Interaction.Triggers>
            <i:EventTrigger EventName="TextChanged">
                <interactivity:ScrollToBottomAction/>
            </i:EventTrigger>
     </i:Interaction.Triggers>
</RichTextBox>

回答by user4860446

RichTextBox.AppendText("String")
RichTextBox.ScrollToCaret()

When I was adding to RichTextBox.text, ScrollToCaret() does not work.

当我添加到 RichTextBox.text 时,ScrollToCaret() 不起作用。

RichTextBox.text = RichTextBox.text + "String"
RichTextBox.ScrollToCaret()