滚动列表视图WPF特定的行 [英] Scroll WPF Listview to specific line

查看:127
本文介绍了滚动列表视图WPF特定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF,Browserlike应用程序。

我有包含一个ListView一页。调用的PageFunction后,我一个行添加到ListView,并要滚动的新行眼帘:

WPF, Browserlike app.
I got one page containing a ListView. After calling a PageFunction I add a line to the ListView, and want to scroll the new line into view:

  ListViewItem item = ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;
  if (item != null)
    ScrollIntoView(item);

这工作。只要新的生产线是针对该行获得焦点像它应该。

This works. As long as the new line is in view the line gets the focus like it should.

问题是,当行是不可见的东西不起作用。

如果该行不可见,对于产生的线没有ListViewItem的,所以ItemContainerGenerator.ContainerFromIndex返回null。

Problem is, things don't work when the line is not visible.
If the line is not visible, there is no ListViewItem for the line generated, so ItemContainerGenerator.ContainerFromIndex returns null.

但是,如果没有这个项目,我怎么滚动行成的看法?有什么办法滚动到最后一行(或任何地方),而无需一个ListViewItem的?

But without the item, how do I scroll the line into view? Is there any way to scroll to the last line (or anywhere) without needing an ListViewItem?

推荐答案

我觉得这里的问题是,如果该行不可见的ListViewItem的是尚未创建。 WPF创建按需可见。

I think the problem here is that the ListViewItem is not created yet if the line is not visible. WPF creates the Visible on demand.

因此​​,在这种情况下,你可能会得到的项目,你呢?
(根据你的评论,你做的)

So in this case you probably get null for the item, do you? (According to your comment, you do)

我已经找到了在该建议访问的ScrollViewer MSDN论坛链接直接中以滚动。对我来说,解决方案presented那里看起来非常像一个黑客,但你可以自己决定。

I have found a link on MSDN forums that suggest accessing the Scrollviewer directly in order to scroll. To me the solution presented there looks very much like a hack, but you can decide for yourself.

下面是从的以上链接:

VirtualizingStackPanel vsp =  
  (VirtualizingStackPanel)typeof(ItemsControl).InvokeMember("_itemsHost",
   BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic, null, 
   _listView, null);

double scrollHeight = vsp.ScrollOwner.ScrollableHeight;

// itemIndex_ is index of the item which we want to show in the middle of the view
double offset = scrollHeight * itemIndex_ / _listView.Items.Count;

vsp.SetVerticalOffset(offset);

这篇关于滚动列表视图WPF特定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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