我怎样才能让一个RichTextBox滚动到最后当我添加一个新行? [英] How can I make a RichTextBox scroll to the end when I add a new line?

查看:379
本文介绍了我怎样才能让一个RichTextBox滚动到最后当我添加一个新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个只读的RichTextBox的是用于登录输出。因为他们是只读他们似乎并不文本时更新自动滚动。我的可以的使用TextChanged事件,迫使滚动结束,但有不只是使滚动发生像正常?

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 can use 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?

推荐答案

我用Google搜索你的问题,发现这个职位。
部分中编程RichTextBox的作者关于让你一直期待什么样的行为已经说明。

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

请检查并让我知道,如果是的任何使用。

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.

这篇关于我怎样才能让一个RichTextBox滚动到最后当我添加一个新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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