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
}
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.
According to this question: TextBox.ScrollToEnd doesn't work when the TextBox is in a non-active tab
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();
EDIT
Example TextBox:
<TextBox TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"
AcceptsReturn="True" Name="textBox"/>