简体   繁体   中英

How can I scroll to the end of a textbox in UWP?

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

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM