滚动的ListViewItem是在ListView的顶 [英] Scroll ListViewItem to be at the top of a ListView

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

问题描述

在WPF中,我知道我可以使用 ListView.ScrollIntoView 滚动特定项目进入视野,但它会一直做滚动的最低量,使产品图所示。

In WPF, I know I can use ListView.ScrollIntoView to scroll a particular item into view, but it will always do the least amount of scrolling so that the item is shown.

我怎样才能使它滚动,这样我想展示的项目滚动到ListView的顶部?

How can I make it scroll so that the item I want to show is scrolled to the top of the ListView?

我想过打电话ScrollIntoView两次,一次是因为我想在顶部的项目,一旦最后显示的项目,但我不知道怎么找出最后一个项列示。

I've thought about calling ScrollIntoView twice, once for the item I want at the top, and once for the last shown item, but I don't know how to find out what the last shown item.

推荐答案

我们可以通过获得的<一个做到这一点href=\"http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.aspx\">ScrollViewer那就是在ListView的控件模板present。如果你有访问的ScrollViewer然后有很多不同的滚动方式暴露出来。

We can do this by obtaining the ScrollViewer that is present in the ListView's ControlTemplate. If you have access to a ScrollViewer then there are a lot of different scrolling methods exposed.

首先,我们可以创造我们想要这个效果添加到ListView:

First, we can create a ListView that we want to add this effect to:

<ListView ItemsSource="{Binding Percents}"
	  SelectionChanged="OnSelectionChanged"
	  x:Name="uiListView"/>


public List<int> Percents { get; set; }

public Window1()
{
	InitializeComponent();

	Percents = new List<int>();
	for (int i = 1; i <= 100; i++)
	{
		Percents.Add(i);
	}
	this.DataContext = this;
}

我们还需要的东西,我们可以用它来从ListView中获取的ScrollViewer。我用类似这样的东西之前,使用自定义的滚动工作,我们可以在这里使用它。

We will also need something that we can use to obtain the ScrollViewer from the ListView. I've used something similar to this before to work with custom scrolling, and we can use it here as well.

public static DependencyObject GetScrollViewer(DependencyObject o)
{
	if (o is ScrollViewer)
	{ return o; }

	for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++)
	{
		var child = VisualTreeHelper.GetChild(o, i);

		var result = GetScrollViewer(child);
		if (result == null)
		{
			continue;
		}
		else
		{
			return result;
		}
	}

	return null;
}

现在,我们只需要处理SelectionChanged事件。因为我们正试图将项目滚动到列表顶部,最好的选择是滚动的底部,然后向上再滚动到我们选择的项目。正如你所说,ScrollIntoView会,直到该项目是可见的只是滚动,所以一旦选定项目到达顶部,因为它滚动备份,它将停止与我们选择的项目让我们在列表的顶端。

Now, we just need to handle the SelectionChanged event. Because we are trying to scroll an item to the top of the list, the best option is to scroll to the bottom, and then re-scroll up to our selected item. As you said, ScrollIntoView will scroll just until the item is visible, and so once the selected item reaches the top as it scrolls back up, it will cease leaving us with our selected item at the very top of the list.

private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
	ScrollViewer scrollViewer = GetScrollViewer(uiListView) as ScrollViewer;
	scrollViewer.ScrollToBottom();

	uiListView.ScrollIntoView(e.AddedItems[0]);
}

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

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