【问题标题】:How can I scroll to the end of a textbox in UWP?如何在 UWP 中滚动到文本框的末尾?
【发布时间】:2020-10-09 17:33:55
【问题描述】:

我正在做一个项目,并决定使用 UWP 而不是使用 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 都会自动滚动到最后?

我在这里找到了几个解决方案,但它们似乎对我不起作用。即这些:

  1. Solution 1
  2. Solution 2

如果我能得到任何帮助,我们将不胜感激

【问题讨论】:

  • 您好,如果答案有效,请接受anfd投票,以便其他人可以方便地找到答案。谢谢。

标签: c# user-interface uwp textbox


【解决方案1】:

事实上,解决方案 2 的 answeranswer 都有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多