<bdo id='rLXgv'></bdo><ul id='rLXgv'></ul>
<i id='rLXgv'><tr id='rLXgv'><dt id='rLXgv'><q id='rLXgv'><span id='rLXgv'><b id='rLXgv'><form id='rLXgv'><ins id='rLXgv'></ins><ul id='rLXgv'></ul><sub id='rLXgv'></sub></form><legend id='rLXgv'></legend><bdo id='rLXgv'><pre id='rLXgv'><center id='rLXgv'></center></pre></bdo></b><th id='rLXgv'></th></span></q></dt></tr></i><div id='rLXgv'><tfoot id='rLXgv'></tfoot><dl id='rLXgv'><fieldset id='rLXgv'></fieldset></dl></div>

    <tfoot id='rLXgv'></tfoot>
      <legend id='rLXgv'><style id='rLXgv'><dir id='rLXgv'><q id='rLXgv'></q></dir></style></legend>

      1. <small id='rLXgv'></small><noframes id='rLXgv'>

        Windows 10 ScrollIntoView() 没有滚动到列表视图中间的项目

        Windows 10 ScrollIntoView() is not scrolling to the items in the middle of a listview(Windows 10 ScrollIntoView() 没有滚动到列表视图中间的项目)

        <tfoot id='GhvBZ'></tfoot>

        <small id='GhvBZ'></small><noframes id='GhvBZ'>

        • <i id='GhvBZ'><tr id='GhvBZ'><dt id='GhvBZ'><q id='GhvBZ'><span id='GhvBZ'><b id='GhvBZ'><form id='GhvBZ'><ins id='GhvBZ'></ins><ul id='GhvBZ'></ul><sub id='GhvBZ'></sub></form><legend id='GhvBZ'></legend><bdo id='GhvBZ'><pre id='GhvBZ'><center id='GhvBZ'></center></pre></bdo></b><th id='GhvBZ'></th></span></q></dt></tr></i><div id='GhvBZ'><tfoot id='GhvBZ'></tfoot><dl id='GhvBZ'><fieldset id='GhvBZ'></fieldset></dl></div>

            <bdo id='GhvBZ'></bdo><ul id='GhvBZ'></ul>

            1. <legend id='GhvBZ'><style id='GhvBZ'><dir id='GhvBZ'><q id='GhvBZ'></q></dir></style></legend>

                  <tbody id='GhvBZ'></tbody>
                  本文介绍了Windows 10 ScrollIntoView() 没有滚动到列表视图中间的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个包含 20 个项目的列表视图.我想以编程方式滚动 Listview.

                  I have a Listview with 20 items in it. I want to scroll the Listview programmatically.

                  ListView?.ScrollIntoView(ListView.Items[0])
                  

                  将列表视图滚动到第一项.

                  will scroll the listview to the first item.

                  ListView?.ScrollIntoView(ListView.Items.Count - 1)
                  

                  将列表视图滚动到页面底部.

                  will scroll the listview to the bottom of the page.

                  但是,我无法使用相同的功能将列表视图滚动到中间的项目.

                  However, I am unable to use the same function to scroll the listview to an item in middle.

                  Eg: ListView?.ScrollIntoView(ListView.Items[5])
                  

                  应该滚动并带我到列表的第 5 项.但它却把我带到了列表的第一项.

                  should scroll and take me to the 5th item of the list. But instead its taking me to the first item of the list.

                  如果可以通过一些变通方法来实现此行为会很好吗?

                  Would be great if this behaviour can be achieved with some workaround?

                  推荐答案

                  我认为您正在寻找的是一种将元素实际 scrollListView 顶部的方法代码>.

                  I think what you are looking for is a method to actually scroll an element to the top of the ListView.

                  在这篇文章中,我创建了一个滚动到 ScrollViewer 中特定元素的扩展方法.

                  In this post, I created an extension method that scrolls to a particular element within a ScrollViewer.

                  你的想法是一样的.

                  您需要首先在 ListView 中找到 ScrollViewer 实例,然后是要滚动到的实际项目,即 ListViewItem.

                  You need to first find the ScrollViewer instance within your ListView, then the actual item to scroll to, that is, a ListViewItem.

                  这是获取ScrollViewer的扩展方法.

                  public static ScrollViewer GetScrollViewer(this DependencyObject element)
                  {
                      if (element is ScrollViewer)
                      {
                          return (ScrollViewer)element;
                      }
                  
                      for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
                      {
                          var child = VisualTreeHelper.GetChild(element, i);
                  
                          var result = GetScrollViewer(child);
                          if (result == null)
                          {
                              continue;
                          }
                          else
                          {
                              return result;
                          }
                      }
                  
                      return null;
                  }
                  

                  获得 ScrollViewer 实例后,我创建了另外两个扩展方法,分别根据其索引或附加对象滚动到项目.由于 ListViewGridView 共享相同的基类 ListViewBase.这两个扩展方法也应该适用于 GridView.

                  Once I get the ScrollViewer instance, I have created two more extension methods to scroll to an item based on its index or attached object respectively. Since ListView and GridView are sharing the same base class ListViewBase. These two extension methods should also work for GridView.

                  基本上,这些方法将首先找到该项目,如果它已经渲染,然后立即滚动到它.如果item为null,则表示虚拟化开启,item尚未实现.所以首先要实现item,调用ScrollIntoViewAsync(基于任务的方法来包装内置的ScrollIntoView,和ChangeViewAsync一样,提供了很多更清晰的代码),计算位置并保存.因为现在我知道要滚动到的位置,所以我需要先将项目一直滚动到它之前的位置立即(即没有动画),然后最后用动画滚动到所需的位置.

                  Basically, the methods will first find the item, if it's already rendered, then scroll to it right away. If the item is null, it means the virtualization is on and the item has yet to be realized. So to realize the item first, call ScrollIntoViewAsync (task-based method to wrap the built-in ScrollIntoView, same as ChangeViewAsync, which offers much cleaner code), calculate the position and save it. Since now I know the position to scroll to, I need to first scroll the item all the way back to its previous position instantly (i.e. without animation), and then finally scroll to the desired position with animation.

                  public async static Task ScrollToIndex(this ListViewBase listViewBase, int index)
                  {
                      bool isVirtualizing = default(bool);
                      double previousHorizontalOffset = default(double), previousVerticalOffset = default(double);
                  
                      // get the ScrollViewer withtin the ListView/GridView
                      var scrollViewer = listViewBase.GetScrollViewer();
                      // get the SelectorItem to scroll to
                      var selectorItem = listViewBase.ContainerFromIndex(index) as SelectorItem;
                  
                      // when it's null, means virtualization is on and the item hasn't been realized yet
                      if (selectorItem == null)
                      {
                          isVirtualizing = true;
                  
                          previousHorizontalOffset = scrollViewer.HorizontalOffset;
                          previousVerticalOffset = scrollViewer.VerticalOffset;
                  
                          // call task-based ScrollIntoViewAsync to realize the item
                          await listViewBase.ScrollIntoViewAsync(listViewBase.Items[index]);
                  
                          // this time the item shouldn't be null again
                          selectorItem = (SelectorItem)listViewBase.ContainerFromIndex(index);
                      }
                  
                      // calculate the position object in order to know how much to scroll to
                      var transform = selectorItem.TransformToVisual((UIElement)scrollViewer.Content);
                      var position = transform.TransformPoint(new Point(0, 0));
                  
                      // when virtualized, scroll back to previous position without animation
                      if (isVirtualizing)
                      {
                          await scrollViewer.ChangeViewAsync(previousHorizontalOffset, previousVerticalOffset, true);
                      }
                  
                      // scroll to desired position with animation!
                      scrollViewer.ChangeView(position.X, position.Y, null);
                  }
                  
                  public async static Task ScrollToItem(this ListViewBase listViewBase, object item)
                  {
                      bool isVirtualizing = default(bool);
                      double previousHorizontalOffset = default(double), previousVerticalOffset = default(double);
                  
                      // get the ScrollViewer withtin the ListView/GridView
                      var scrollViewer = listViewBase.GetScrollViewer();
                      // get the SelectorItem to scroll to
                      var selectorItem = listViewBase.ContainerFromItem(item) as SelectorItem;
                  
                      // when it's null, means virtualization is on and the item hasn't been realized yet
                      if (selectorItem == null)
                      {
                          isVirtualizing = true;
                  
                          previousHorizontalOffset = scrollViewer.HorizontalOffset;
                          previousVerticalOffset = scrollViewer.VerticalOffset;
                  
                          // call task-based ScrollIntoViewAsync to realize the item
                          await listViewBase.ScrollIntoViewAsync(item);
                  
                          // this time the item shouldn't be null again
                          selectorItem = (SelectorItem)listViewBase.ContainerFromItem(item);
                      }
                  
                      // calculate the position object in order to know how much to scroll to
                      var transform = selectorItem.TransformToVisual((UIElement)scrollViewer.Content);
                      var position = transform.TransformPoint(new Point(0, 0));
                  
                      // when virtualized, scroll back to previous position without animation
                      if (isVirtualizing)
                      {
                          await scrollViewer.ChangeViewAsync(previousHorizontalOffset, previousVerticalOffset, true);
                      }
                  
                      // scroll to desired position with animation!
                      scrollViewer.ChangeView(position.X, position.Y, null);
                  }
                  
                  public static async Task ScrollIntoViewAsync(this ListViewBase listViewBase, object item)
                  {
                      var tcs = new TaskCompletionSource<object>();
                      var scrollViewer = listViewBase.GetScrollViewer();
                  
                      EventHandler<ScrollViewerViewChangedEventArgs> viewChanged = (s, e) => tcs.TrySetResult(null);
                      try
                      {
                          scrollViewer.ViewChanged += viewChanged;
                          listViewBase.ScrollIntoView(item, ScrollIntoViewAlignment.Leading);
                          await tcs.Task;
                      }
                      finally
                      {
                          scrollViewer.ViewChanged -= viewChanged;
                      }
                  }
                  
                  public static async Task ChangeViewAsync(this ScrollViewer scrollViewer, double? horizontalOffset, double? verticalOffset, bool disableAnimation)
                  {
                      var tcs = new TaskCompletionSource<object>();
                  
                      EventHandler<ScrollViewerViewChangedEventArgs> viewChanged = (s, e) => tcs.TrySetResult(null);
                      try
                      {
                          scrollViewer.ViewChanged += viewChanged;
                          scrollViewer.ChangeView(horizontalOffset, verticalOffset, null, disableAnimation);
                          await tcs.Task;
                      }
                      finally
                      {
                          scrollViewer.ViewChanged -= viewChanged;
                      }
                  }
                  

                  <小时>

                  您还可以通过指定第二个参数来使用 ScrollIntoView 的新重载,以确保项目在顶部边缘对齐;但是,这样做并没有我以前的扩展方法中的平滑滚动过渡.

                  You can also use the new overload of ScrollIntoView by specifying the second parameter to make sure the item is aligned on the top edge; however, doing so doesn't have the smooth scrolling transition in my previous extension methods.

                  MyListView?.ScrollIntoView(MyListView.Items[5], ScrollIntoViewAlignment.Leading);
                  

                  这篇关于Windows 10 ScrollIntoView() 没有滚动到列表视图中间的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C# namespace alias - what#39;s the point?(C# 命名空间别名 - 有什么意义?)
                  Using Xpath With Default Namespace in C#(在 C# 中使用具有默认命名空间的 Xpath)
                  Generating an EDMX from a DB2 Database(从 DB2 数据库生成 EDMX)
                  IBM .NET Data Provider Connection String issue with Library List(库列表的 IBM .NET 数据提供程序连接字符串问题)
                  .NET DB2 OLEDB pre-requisites(.NET DB2 OLEDB 先决条件)
                  Referring to Code in IBM.Data.DB2 makes that Assembly Unavailable to the rest of my Solution(引用 IBM.Data.DB2 中的代码使该程序集对我的解决方案的其余部分不可用)
                  <legend id='Qte9O'><style id='Qte9O'><dir id='Qte9O'><q id='Qte9O'></q></dir></style></legend>
                    <tbody id='Qte9O'></tbody>
                  <tfoot id='Qte9O'></tfoot>
                1. <small id='Qte9O'></small><noframes id='Qte9O'>

                        • <bdo id='Qte9O'></bdo><ul id='Qte9O'></ul>
                        • <i id='Qte9O'><tr id='Qte9O'><dt id='Qte9O'><q id='Qte9O'><span id='Qte9O'><b id='Qte9O'><form id='Qte9O'><ins id='Qte9O'></ins><ul id='Qte9O'></ul><sub id='Qte9O'></sub></form><legend id='Qte9O'></legend><bdo id='Qte9O'><pre id='Qte9O'><center id='Qte9O'></center></pre></bdo></b><th id='Qte9O'></th></span></q></dt></tr></i><div id='Qte9O'><tfoot id='Qte9O'></tfoot><dl id='Qte9O'><fieldset id='Qte9O'></fieldset></dl></div>