c# - 将新行添加到 XAML 中的 TextBlock 时,将 ScrollViewer 滚动到 WPF 中的底部

标签 c# wpf scrollviewer eventtrigger

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

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

XAML

<ScrollViewer Name="ServerScroll"
              VerticalScrollBarVisibility="Auto">
    <TextBlock Name="serverConsole"
               Margin="5"
               Background="White"
               TextWrapping="Wrap"/>
</ScrollViewer>

代码隐藏

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

最佳答案

使用 TextBox,响应 TextBox.TextChanged

如果每次更改 TextBlockText 属性时都想滚动到末尾,我建议切换到 TextBox,这样您可以使用 System.Windows.Interactivity 连接到它的 TextChanged 事件:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Width="50" Height="25" Click="Button_Click"></Button>
        <ScrollViewer Grid.Row="1" Name="ServerScroll"
                      VerticalScrollBarVisibility="Auto">
            <TextBox Name="serverConsole"
                       Margin="5"
                       Background="White"
                       TextWrapping="Wrap">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="TextChanged">
                        <ei:CallMethodAction MethodName="ScrollToEnd" TargetObject="{Binding ElementName=ServerScroll}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </TextBox>
        </ScrollViewer>
    </Grid>
</Window>

使用 TextBlock,响应 Button.Click

如果您希望在单击 Button 时滚动到末尾,您可以使用相同的技术连接到它的 Click 事件:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Width="50" Height="25" Click="Button_Click">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <ei:CallMethodAction MethodName="ScrollToEnd" TargetObject="{Binding ElementName=ServerScroll}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
        <ScrollViewer Grid.Row="1" Name="ServerScroll"
                      VerticalScrollBarVisibility="Auto">
            <TextBlock Name="serverConsole"
                       Margin="5"
                       Background="White"
                       TextWrapping="Wrap">
            </TextBlock>
        </ScrollViewer>
    </Grid>
</Window>

使用 ListView,响应 CollectionChanged

看起来您真的想使用 ItemsControl 而不是 TextBlock,因为您在谈论条目和所有内容。您可以切换到 ListView 并连接到它的 CollectionChanged 事件:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Width="50" Height="25" Click="Button_Click"/>

        <ScrollViewer Grid.Row="1" Name="ServerScroll"
                      VerticalScrollBarVisibility="Auto">
            <ListView x:Name="listView" ItemsSource="{Binding MyList}">
                <i:Interaction.Triggers>
                    <i:EventTrigger SourceObject="{Binding MyList}" EventName="CollectionChanged">
                        <ei:CallMethodAction MethodName="ScrollToEnd" TargetObject="{Binding ElementName=ServerScroll}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ListView>
        </ScrollViewer>
    </Grid>
</Window>

在你的 View 模型中:

public ObservableCollection<string> MyList { get; } = new ObservableCollection<string>();

关于c# - 将新行添加到 XAML 中的 TextBlock 时,将 ScrollViewer 滚动到 WPF 中的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58830873/

相关文章:

c# - 如何序列化附加属性

c# - 绑定(bind)到 ScrollViewer 的 ViewportWidth 和 ViewportHeight

WPF GridView 标题不会水平滚动

c# - 在 C# 中将 SqlXml 转换为 XmlDocument 以获得 SQLCLR 函数的快速方法?

c# - 为什么我的 C# AJAX 请求在 HTTP 下可以正常工作,而在 HTTPS 下却失败?

c# - 我可以确定从 .csproj 或 .targets 文件构建时使用的 .NET SDK 版本吗?

c# - 如何在 RichTextBox 中键入半空格?

c# - 找不到元数据文件 '.dll'

c# - WPF 访问 ListView 代码隐藏的滚动查看器

c# - 有没有办法限制谁可以继承类或接口(interface)?