我在点击按钮时从代码后面添加了一些文本,但这不是自动滚动。

示例如下。

XAML:

<StackPanel>
    <TextBox Name="TestTextBox" AcceptsReturn="True" Text="test" MaxHeight="60" TextWrapping="Wrap"/>
    <Button Content="Add text" Tapped="Button_Tapped"  />
</StackPanel>


代码背后:

 int i = 0;
 private void Button_Tapped(object sender, TappedRoutedEventArgs e)
 {
     TestTextBox.Text = TestTextBox.Text + " newtest " + i++;
     TestTextBox.SelectionStart = TestTextBox.Text.Length;
 }


有什么方向解决这个问题吗?

最佳答案

使用NewLine属性并设置VerticalScrollBarVisibility

<StackPanel>
    <TextBox  ScrollViewer.VerticalScrollBarVisibility="Auto" Name="TestTextBox" AcceptsReturn="True" Text="test"  MaxHeight="60" TextWrapping="Wrap">
    </TextBox>
    <Button Content="Add text" Tapped="Button_Tapped"  />
</StackPanel>


代码隐藏

 int i = 0;
 private void Button_Tapped(object sender, TappedRoutedEventArgs e)
 {
    TestTextBox.Text = TestTextBox.Text + " newtest " + i++ + Environment.NewLine;
    TestTextBox.SelectionStart = TestTextBox.Text.Length;
 }

10-06 04:09