18

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

4

4 回答 4

21

我搜索了您的问题并找到了这篇文章。在“Programming the 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();
        }
    }
}

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

于 2012-04-25T04:57:31.923 回答
17

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

动作如下所示:

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>
于 2014-01-20T16:11:23.107 回答
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);
        }
    }
}
于 2021-10-08T01:54:16.690 回答
-1
RichTextBox.AppendText("String")
RichTextBox.ScrollToCaret()

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

RichTextBox.text = RichTextBox.text + "String"
RichTextBox.ScrollToCaret()
于 2015-05-03T23:23:44.207 回答