c# - 计算水平偏移以将 ListView 滚动到 SelectedItem 的中心

标签 c# listview windows-store-apps windows-8.1 scrollviewer

我正在构建一个照片应用程序,使用 FlipViewlistView 作为分页。 当我单击 ListView 中的缩略图时,它会在 FlipView 中显示相同的图片。当我滑入 FlipView 时,所选的任何照片都会在 ListView 中选择相同的图片。这是通过添加到它们两个来完成的:

ListView:

SelectedIndex="{Binding Path=SelectedIndex, ElementName=flipView1, Mode=TwoWay}

对于 FlipView:

SelectedIndex="{Binding Path=SelectedIndex, ElementName=listView1, Mode=TwoWay}

我在 ListView SelectionChanged 事件中添加了:

 if (e.AddedItems.Count > 0)
        listView1.ScrollIntoView(e.AddedItems.First(), ScrollIntoViewAlignment.Leading);

我唯一的问题是,当我滑动 FlipView 时,所需的图片在 ListView 中被选中,但是 ScrollViewer 没有滚动到它。我尝试使用 WinRTXamlToolkit 更改 ScrollViewer 的位置:

private void pageRoot_Loaded()
        {
            // count number of all items
            int itemCount = this.listView1.Items.Count;
            if (itemCount == 0)
                return;

            if (listView1.SelectedIndex >= itemCount)
                listView1.SelectedIndex = itemCount - 1;

            // calculate x-posision of selected item
            double listWidth = this.listView1.ActualWidth;
            double xPos = (listWidth / itemCount) * listView1.SelectedIndex;

            // scroll
            var scrollViewer2 = listView1.GetFirstDescendantOfType<ScrollViewer>();
            if (scrollViewer2 != null)
                scrollViewer2.ChangeView(xPos, 0.0, 1);
        }

第一次 listWidth1600.0 然后它一直变成 0.0,这给 xPos = 0.0!

我该如何解决这个问题?

最佳答案

https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.listview.aspx

您应该使用两种“ScrollIntoView”方法中的一种。

关于c# - 计算水平偏移以将 ListView 滚动到 SelectedItem 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29841841/

相关文章:

c# - 如何避免重复订阅事件?

c# - 如何重定向到 asp.net core razor 页面(无路由)

c# - 条形码图像到 C# 中的 Code39 转换?

android - 关注listview的第一条记录

c# - 我如何创建 LiveConnectClient 的实例?

c# - 可为空的 int 特定于 String() 的文化

wpf - GroupStyles 是如何工作的?

java - listview 的 onItemClick 和 case 内的循环

c# - 在漫游设置中存储复杂数据类型

c# - 如何在 Windows 应用商店应用程序中为按钮位置(边距)设置动画?