TextBox.ScrollToEnd doesn't work when the Text

2019-05-10 11:01发布

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

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?

2条回答
贼婆χ
2楼-- · 2019-05-10 11:29

Set the CaretIndex:

   if (tb != null) 
   { 
       tb.AppendText(e.Data + Environment.NewLine); 
       tb.CaretIndex = tb.Text.Length;
       tb.ScrollToEnd(); 
   } 
查看更多
forever°为你锁心
3楼-- · 2019-05-10 11:39

Looks like a bug... you should report it on Microsoft Connect

查看更多
登录 后发表回答