如何在 UWP 中滚动到文本框的末尾?
How can I scroll to the end of a textbox in UWP?
我正在做一个项目,并决定使用 UWP 而不是使用 winforms(我更习惯使用 winforms)。我有一个非常简单的 TextBox
,我用它来记录发送和接收的数据。这是一个简单的示例:
Textbox view
这是它的 XAML 代码:
<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>
目前,我的 TextBox
被我第一次启动项目时得到的默认值 Grid
包装。
下面是用于向 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}";
}
正如在此 image 中所见,正在记录的数据现在大于 TextBox
的高度。无论如何我可以做到这样每次调用方法 Log
时,LogsTextBox
都会自动滚动到末尾?
我在这里找到了几个解决方案,但它们似乎对我不起作用。即这些:
- Solution 1
如果我能得到任何帮助,我将不胜感激
事实上,解决方案 2 的 and 都有效。
我正在做一个项目,并决定使用 UWP 而不是使用 winforms(我更习惯使用 winforms)。我有一个非常简单的 TextBox
,我用它来记录发送和接收的数据。这是一个简单的示例:
Textbox view
这是它的 XAML 代码:
<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>
目前,我的 TextBox
被我第一次启动项目时得到的默认值 Grid
包装。
下面是用于向 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}";
}
正如在此 image 中所见,正在记录的数据现在大于 TextBox
的高度。无论如何我可以做到这样每次调用方法 Log
时,LogsTextBox
都会自动滚动到末尾?
我在这里找到了几个解决方案,但它们似乎对我不起作用。即这些:
- Solution 1
如果我能得到任何帮助,我将不胜感激
事实上,解决方案 2 的