具有自动垂直滚动的多行文本框

具有自动垂直滚动的多行文本框

本文介绍了具有自动垂直滚动的多行文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Internet上有很多类似的问题,包括SO,但是在我看来,建议的解决方案不起作用.场景:xaml中有一个日志文本框

There are a lot of similiar questions over internet, on SO included, but proposed solutions doesn't work in my case.Scenario : there is a log textbox in xaml

 <TextBox Name="Status"
          Margin="5"
          Grid.Column="1"
          Grid.Row="5"
          HorizontalAlignment="Left"
          VerticalAlignment="Top"
          Width="600"
          Height="310"/>

代码隐藏中的一些方法可以完成一些工作,并在此文本框中添加一些多行(也许是问题所在?)消息:

There are methods in code-behind that do some work and add some multiline (maybe that's the problem?) messages into this textbox:

private static void DoSomeThings(TextBox textBox)
{
   // do work
   textBox.AppendText("Work finished\r\n"); // better way than Text += according to msdn
   // do more
   textBox.AppendText("One more message\r\n");
   ...
}

private static void DoSomething2(TextBox textBox)
{
   // same as first method
}

发生所有操作后,需要滚动到文本框的底部.尝试使用ScrollToEnd(),ScrollToLine,将文本框包装到ScrollViewer,选择和插入符解决方法中,将ScrollToEnd附加到TextChanged.在溢出文本框高度的执行行仍然需要手动滚动到之后,这些都不起作用.对不起,重复的问题,我想我缺少一些较小的问题,可以由对此问题有新见解的人快速解决.预先感谢.

Need to scroll to bottom of textbox after all actions take place. Tried ScrollToEnd(), ScrollToLine, wrapping textbox into ScrollViewer, Selection and Caret workarounds, attaching ScrollToEnd to TextChanged. None of this works, after execution lines that overflow textbox height still need to be scrolled to manually. Sorry for duplicate question, i guess i'm missing some minor issues that can be resolved quickly by someone that has fresh vision on the problem. Thanks in advance.

推荐答案

根据此问题:

您必须将文本框对准焦点,更新插入符号的位置,然后滚动到结尾:

You have to focus the text box, update the caret position and then scroll to end:

Status.Focus();
Status.CaretIndex = Status.Text.Length;
Status.ScrollToEnd();

编辑

示例文本框:

<TextBox TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"
         AcceptsReturn="True" Name="textBox"/>

这篇关于具有自动垂直滚动的多行文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:22