UWP textBox在收到文本时自动向下滚动

时间:2019-05-16 08:48:06

标签: c# uwp textbox scrollview

我在UWP中有一个简单的文本框,其中自动添加了内容。问题是,当文本到达文本框底部时,除非我使用鼠标向下滚动,否则内容仍会添加但变得不可见。

如何向文本框添加自动向下滚动功能?

尝试ScrollViewer,但没有运气。

    <TextBox x:Name="mainTextBox" Grid.Row="2" Grid.Column="0" Margin="5" ScrollViewer.VerticalScrollMode="Auto" TextWrapping="Wrap" Text="" IsReadOnly="True" />

以下是更新我的文本框的代码:

    private void UpdateStatus(string strMessage, NotifyType type)
    {
        mainTextBox.Text += string.IsNullOrEmpty(mainTextBox.Text) ? strMessage : "\n" + strMessage;

        var peer = Windows.UI.Xaml.Automation.Peers.FrameworkElementAutomationPeer.FromElement(mainTextBox);
        if (peer != null)
        {
            peer.RaiseAutomationEvent(Windows.UI.Xaml.Automation.Peers.AutomationEvents.LiveRegionChanged);
        }
    }

该怎么办?有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

这是这篇文章的副本:

UWP C# Scroll to the bottom of TextBox

我已经测试过,并且效果很好..:)