当 TextBox 位于非活动选项卡中时,TextBox.ScrollToEnd 不起作用 [英] TextBox.ScrollToEnd doesn't work when the TextBox is in a non-active tab
问题描述
我们的应用程序启动几个后台进程并将它们的输出放入 TextBoxes - 每个在 TabControl 中的一个单独的 TabItem 中.我希望TextBoxes自动滚动显示最后的输出行,所以在将输出/错误行添加到文本框的数据处理函数中,我也调用了TextBox.ScrollToEnd():
Our application starts several background processes and put their output into TextBoxes - each in a separate TabItem in a TabControl. I want the TextBoxes to automatically scroll to show the last output line, so in the data handling function that adds the output/error line to the text box, I also call TextBox.ScrollToEnd():
void OnServerProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
Dispatcher.Invoke(new Action(() =>
{
TextBox tb = getServerProcessOutputTextBox(sender);
if (tb != null)
{
tb.AppendText(e.Data + Environment.NewLine);
tb.ScrollToEnd();
}
}));
}
}
这对于活动选项卡中的 TextBox 非常有用,但是当我切换到另一个选项卡时,我发现它没有向下滚动到最后.
This works great for the TextBox in the active tab, but when I switch to another tab, I see that it wasn't scrolled down to the end.
这是一个已知问题吗?有办法解决吗?
Is this a known problem? Is there a way to fix it?
推荐答案
设置 CaretIndex:
Set the CaretIndex:
if (tb != null)
{
tb.AppendText(e.Data + Environment.NewLine);
tb.CaretIndex = tb.Text.Length;
tb.ScrollToEnd();
}
这篇关于当 TextBox 位于非活动选项卡中时,TextBox.ScrollToEnd 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!