我正在尝试做一些棘手的事情。我想通过确定是否ScrollViewer
滚动以及该项目是否位于ScrollViewer
. 当用户手动将其滚动到 ListView 的中心时,我想选择一个项目。我不想滚动到所选项目,因为那太容易了,对吧?
这是一张图:
我设法检测到滚动条是否已滚动。首先,我在有问题的 ListView 上放置一个 Loaded 事件“itemsList”,并ScrollViewer
通过遍历可视化树来查找类型以查看它是否已完成滚动并将其标记ScrollViewer
到 ListView 并处理 SelectionChanged 事件:
private void ItemsList_OnLoaded(object sender, RoutedEventArgs e)
{
var listView = (sender as ListView);
if (listView != null)
{
var scrollers = FindVisualChildren<ScrollViewer>(sender as DependencyObject);
var scrollView = scrollers.First();
if (scrollView != null)
{
scrollView.Tag = listView;
scrollView.ViewChanged += ScrollViewOnViewChanged;
listView.SelectionChanged += listView_SelectionChanged;
}
}
}
public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
我处理ScrollViewOnViewChanged
确定滚动查看器是否仍在滚动的事件。这是我遇到一些问题的地方,我还需要确定ListViewItem
并选择它的位置 - 我希望在这里得到一些帮助:
private void ScrollViewOnViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
//Debug.WriteLine("ScrollViewOnViewChanged + IsIntermediate="+e.IsIntermediate);
var scrollviewer = sender as ScrollViewer;
var listview = scrollviewer.Tag as ListView;
if (!e.IsIntermediate)
{
Debug.WriteLine("IsIntermediate = false");
// TODO: Determine the center Item and set is as the selected item for this group.
}
}
然后是SelectionChanged
我创造奇迹的事件:
void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("SelectionChanged");
}
谢谢!任何帮助,将不胜感激!