小编bok*_*oki的帖子

当向 XAML 中的 TextBlock 添加新行时,将 ScrollViewer 滚动到 WPF 中的底部

我试图将垂直滚动条保留在底部(最新条目),但目前,滚动条只是停留在同一个位置,因此当内容被添加到字符串中时,滚动条会移动到顶部。

我知道我可以使用ServerScroll.ScrollToEnd()代码隐藏中的属性将栏移动到末尾。但是有没有办法用xaml自动做到这一点?(这样我就不必每次添加到字符串时都调用此属性)。

XAML

<ScrollViewer Name="ServerScroll"
              VerticalScrollBarVisibility="Auto">
    <TextBlock Name="serverConsole"
               Margin="5"
               Background="White"
               TextWrapping="Wrap"/>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

代码隐藏

private void example_Click(object sender, RoutedEventArgs e)
{
    ServerConsole += "asdf\r\n";      // binded to TextBlock
    ServerScroll.ScrollToEnd();    
}
Run Code Online (Sandbox Code Playgroud)

c# wpf eventtrigger scrollviewer

5
推荐指数
1
解决办法
3205
查看次数

标签 统计

c# ×1

eventtrigger ×1

scrollviewer ×1

wpf ×1