1

I'm working on a project and have decided to use UWP instead of using winforms (which I am more comfortable with). I have a very simple TextBox that I am using as a way to log data being sent and received. Here is a simple example of what it looks like:

Textbox view

Here is what the XAML code for it:

<Grid>
    <TextBlock HorizontalAlignment="Left" Margin="18,862,0,0" Text="Logs " TextWrapping="Wrap" VerticalAlignment="Top"/>
    <TextBox x:Name="LogsTextBox" Margin="18,886,18,0" Text="Welcome to Tiger" TextWrapping="Wrap" VerticalAlignment="Top" Height="88" FontFamily="Courier New" FontSize="12" AcceptsReturn="True" ScrollViewer.VerticalScrollBarVisibility="Visible" IsReadOnly="True"/>
</Grid>

At the current moment of time, my TextBox is wrapped by the default Grid that I got when I first started the project.

Here is a look at the method used to add lines to the LogsTextBox

/// <summary>
/// A method used to log data to the LogsTextBox
/// </summary>
/// <param name="LogString">The string to be logged</param>
private void Log(string LogString)
{
    string TimeString = DateTime.Now.ToString("hh-mm-ss-tt");
    LogsTextBox.Text += $"\n[{TimeString}]: {LogString}";
}

As can be seen in this image the data being logged is now larger than the height of the TextBox. Is there anyway I could make it so that every time the method Log is called, the LogsTextBox would auto scroll to the end?

I've found a couple of solutions on here but they didnt seeme to work for me. Namely these:

  1. Solution 1
  2. Solution 2

Any help I could get would be greatly appreciated

  • Hi, If the answer works, please accept anfd vote, in order others can find the answer conveniently. thanks. – Vincent Jun 22 '20 at 08:34

1 Answers1

0

As a matter of fact, solution 2's answer and answer both works.

enter image description here

Vincent
  • 3,124
  • 3
  • 21
  • 40