让 ListView 滚动到所选项目 [英] Let ListView scroll to selected item

查看:30
本文介绍了让 ListView 滚动到所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WinRT/C#/XAML 应用程序,其视图具有项目的垂直 ListView.根据项目的数量,ListView 会显示一个垂直滚动条.这是 XAML 定义:

I have a WinRT/C#/XAML app with a view that has a vertical ListView of items. Depending on the amount of items the ListView shows a vertical scrollbar. Here's the XAML definition:

<UserControl.Resources>
    <CollectionViewSource
        x:Name="myViewSource"
        Source="{Binding myViewModel.Items}" />
</UserControl.Resources>
...
<ListView
    x:Name="myListView"
    ItemsSource="{Binding Source={StaticResource myViewSource}}"
    SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
</ListView>

现在每次我导航到这个视图时,ListView 的选定项都是通过设置视图模型中的数据绑定 SelectedItem 属性来选择的(OnNavigatedTo).我的问题:ListView 不会自动滚动到这个选定的项目.滚动条保留在 ListView 的顶部,用户必须手动滚动才能看到所选项目.

Now everytime I navigate to this view, the selected item of the ListView is chosen by setting the databound SelectedItem property in the view model from code behind (OnNavigatedTo). My problem: the ListView doesn't scroll automatically to this selected item. The scrollbar remains at the top of the ListView and the user has to scroll manually to see the selected item.

在后面的代码中设置 SelectedItem(在 OnNavigatedTo 中)后,我尝试执行 myListView.ScrollIntoView(MyViewModel.SelectedItem);,但它不起作用.滚动条保持在顶部.

I tried to execute myListView.ScrollIntoView(MyViewModel.SelectedItem); after setting the SelectedItem in the code behind (in OnNavigatedTo), but it doesn't work. The scrollbar remains at the top.

我知道 SO 上的这个线程:将 WinRT ListView 滚动到特定组 .这似乎是一个类似的问题.但是当我手动或使用 WinRT XAML 工具包遍历 ListView 的可视化树时,它没有找到 ScrollViewer(而是返回 null).

I'm aware of this thread on SO: Scroll WinRT ListView to particular group . This seems to be a similar problem. But when I walk the visual tree of the ListView manually or with the WinRT XAML Toolkit, it doesn't find a ScrollViewer (returns null instead).

推荐答案

感谢 Filip 我注意到在 OnNavigatedTo() 中调用 ScrollIntoView() 为时过早,因为ListView 控件尚未加载到此位置.

Thanks to Filip I noticed that calling ScrollIntoView() in OnNavigatedTo() was too early, because the ListView control is not loaded yet in this place.

第一个解决思路是绑定ListView的Loaded事件:

The first solution idea was to bind the Loaded event of the ListView:

myListView.Loaded += (s, e) => 
    myListView.ScrollIntoView(MyViewModel.SelectedItem);

不幸的是,这会导致令人讨厌的视觉效果,其中当前 ListView 项目与所选项目重叠了几秒钟,然后一切都重新排列好了.

Unfortunately that causes a nasty visual effect, where current ListView items overlap with the selected item for parts of a second, before everything is rearranged well.

我找到的最终解决方案是通过视图的Dispatcher异步调用ScrollIntoView():

The final solution I found is to call ScrollIntoView() asynchronously via the Dispatcher of the view:

myListView.Loaded += (s, e) => Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
    () => myListView.ScrollIntoView(MyViewModel.SelectedItem));

使用此解决方案,布局工作正常.

With this solution the layouting works fine.

这篇关于让 ListView 滚动到所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆