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

bok*_*oki 5 c# wpf eventtrigger scrollviewer

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

我知道我可以使用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)

Cor*_*ane 6

使用文本框,做出反应TextBox.TextChanged

Text如果您想在每次更改属性时滚动到末尾TextBlock,我建议切换到 a TextBox,以便您可以TextChanged使用以下方式连接到其事件System.Windows.Interactivity

<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>
Run Code Online (Sandbox Code Playgroud)

使用 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>
Run Code Online (Sandbox Code Playgroud)

使用 ListView,做出反应CollectionChanged

看来您确实想使用 anItemsControl而不是 a TextBlock,因为您正在谈论条目和所有内容。您也可以切换到 aListView并连接到它的事件: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>
Run Code Online (Sandbox Code Playgroud)

在你的视图模型中:

public ObservableCollection<string> MyList { get; } = new ObservableCollection<string>();
Run Code Online (Sandbox Code Playgroud)