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

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

我知道我可以在代码后面使用ServerScroll.ScrollToEnd()属性将小节移到末尾。但是有没有一种方法可以自动执行此操作? (这样我就不必在每次添加到字符串时都调用此属性)。

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();    
}
just483 回答:将新行添加到XAML中的TextBlock时,将ScrollViewer滚动到WPF的底部

使用文本框,对TextBox.TextChanged

做出反应

如果每次更改Text的{​​{1}}属性时都要滚动到末尾,建议您切换到TextBlock,以便可以连接到其{{1 }}使用TextBox的事件:

TextChanged

使用TextBlock,对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> 时滚动到末尾,则可以使用相同的技术来连接到其Button.Click事件:

Button

使用ListView,对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> 而不是CollectionChanged,因为您正在谈论条目和所有内容。您也可以切换到ItemsControl并连接到其TextBlock事件:

ListView

在您的视图模型中:

CollectionChanged
本文链接:https://www.f2er.com/3112604.html

大家都在问