c# - 自动滚动到 WinRT 应用程序中的 ListView 底部

标签 c# listview windows-8 windows-runtime autoscroll

每次将新项目添加到 ItemsSource 时,我都在努力使 ListView 自动向下滚动到底部。根据this post ,我所要做的就是使用以下内容:

private void ScrollToBottom() 
{ 
    var scrollViewer = MyListView.GetFirstDescendantOfType<ScrollViewer>(); 
    scrollViewer.ScrollToVerticalOffset(scrollViewer.ScrollableHeight); 
}

使用 WinRT XAML 工具包。但它没有任何效果我每次从 ListView 的项目集合中添加或删除元素时调用此方法。虽然没有自动滚动。

在 XAML 中,还有 ListView:

<ScrollViewer>
    <ListView x:Name="LinesListView"
        ItemsSource="{Binding Lines}"
        ItemTemplate="{StaticResource LineItemTemplate}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel>
                    <StackPanel.ChildrenTransitions>
                        <TransitionCollection>
                            <EntranceThemeTransition/>
                        </TransitionCollection>
                    </StackPanel.ChildrenTransitions>
                 </StackPanel>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>
</ScrollViewer>

另一方面,在我上面提供的同一链接上还有另一种解决方案,但即使我尝试在 XAML 中指定它,它也不会显示 ListView 项目动画。

我在这里错过了什么?

非常感谢任何建议,谢谢。

最佳答案

终于找到方法了:

我不得不摆脱 ScrollViewer,当添加项目时,首先我必须更新 ListView 的布局,然后向下滚动:

myListView.UpdateLayout();
myListView.ScrollToBottom();

ScrollToBottom(); 方法在 WinRT XAML 工具包 中实现。

很简单,虽然我用许多其他方法来解决它...

动画问题通过 XAML 中的一些更改得到解决:

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel>
            <StackPanel.Transitions>
                <TransitionCollection>
                    <AddDeleteThemeTransition/>
                 </TransitionCollection>
            </StackPanel.Transitions>
        </StackPanel>
    </ItemsPanelTemplate>
</ListView.ItemsPanel>

关于c# - 自动滚动到 WinRT 应用程序中的 ListView 底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17955056/

相关文章:

c# - 显示 WPF TextBox 的剩余字符,该文本框的 MaxLength 受到限制

c# - 在我的应用程序中更改事件选项卡的快捷方式

windows-8 - F# TypeProvider 可以在 Windows 应用商店应用程序中使用吗?

c# - WPF 中的比例最小宽度

c# - 彩色控制台输出

Android ListView使用AsyncTask更新Image Thumbnails导致View回收

android - 将新列表添加到 listView

没有(故意)实现线程的 JavaFX ConcurrentModificationException

c# - 使用 .NET 为 Windows 应用商店应用程序上传文件

delphi - 使用 Delphi 开发 native Metro 应用程序的资源