【问题标题】:How can I make a RichTextBox scroll to the end when I add a new line?添加新行时如何使 RichTextBox 滚动到末尾?
【发布时间】:2012-05-05 17:07:10
【问题描述】:

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

【问题讨论】:

标签: c# wpf xaml richtextbox


【解决方案1】:

我想出了以下针对 wpf Richtextbox 自动滚动的解决方案

public partial class MainWindow
{
    private bool AutoScroll = true;

    public MainWindow()
    {
        InitializeComponent();

        yourRichTextBox.Loaded += (s, e) =>
          {
              var scrollViewer = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(yourRichTextBox, 0), 0) as ScrollViewer;
              scrollViewer.ScrollChanged += (scroller, eScroller) => ScrollViewer_ScrollChanged(scroller, eScroller);
          };
    }

    private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {
        // User scroll event : set or unset autoscroll mode
        if (e.Source as ScrollViewer != null && e.ExtentHeightChange == 0)
        {   // Content unchanged : user scroll event
            if ((e.Source as ScrollViewer).VerticalOffset == (e.Source as ScrollViewer).ScrollableHeight)
            {   // Scroll bar is in bottom
                // Set autoscroll mode
                AutoScroll = true;
            }
            else
            {   // Scroll bar isn't in bottom
                // Unset autoscroll mode
                AutoScroll = false;
            }
        }

        // Content scroll event : autoscroll eventually
        if (AutoScroll && e.ExtentHeightChange != 0 && e.Source as ScrollViewer != null)
        {   // Content changed and autoscroll mode set
            // Autoscroll
            (e.Source as ScrollViewer).ScrollToVerticalOffset((e.Source as ScrollViewer).ExtentHeight);
        }
    }
}

【讨论】:

    【解决方案2】:
    RichTextBox.AppendText("String")
    RichTextBox.ScrollToCaret()
    

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

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

    【讨论】:

    【解决方案3】:

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

    动作如下所示:

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

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

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

    【讨论】:

    • 我无法让它工作,因为混合版本太旧了。我已经尝试了stackoverflow.com/questions/32024034/… 中的建议,但没有成功。有什么想法吗?
    • &lt;ei:CallMethodAction TargetObject="{Binding}" MethodName="ScrollToEnd"/&gt; 也可以。
    【解决方案4】:

    我搜索了您的问题并找到了this 帖子。 在“RichTextBox 编程”一节中,作者描述了如何获得您所期望的行为。

    请检查并告诉我它是否有用。


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

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

    后面的代码如下:

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

    这解决了自动滚动的问题,请检查一下,如果有任何帮助,请告诉我。

    【讨论】:

    • 它使滚动条可见,但是当以编程方式添加文本时它仍然不会滚动到文档的末尾。看起来我可能会遇到 TextChanged 事件。
    • @Merad 你如何处理 TextChanged 事件?
    猜你喜欢
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2011-11-10
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 2012-04-30
    相关资源
    最近更新 更多