【问题标题】:how to programmatically display the bottom row in virtual ListView如何以编程方式在虚拟 ListView 中显示底行
【发布时间】:2013-09-03 20:37:37
【问题描述】:

我在 Winforms 中有一个虚拟 ListView,它可以很好地显示数据库中的内容。项目被异步添加到数据库中,并且当我将滚动条拉到底部时可见。

现在我想确保每当异步线程添加到数据库时,它也会更新 ListView,以便它知道哪个索引应该是底行。在添加 ListView 对象之前,我可以使用 ListBox 来实现,在其中我根据 itemheight 和列表框高度设置哪个索引应该是顶部索引:

int numItems = listBox1.ClientSize.Height / listBox1.ItemHeight;
if (listBox1.TopIndex == listBox1.Items.Count - numItems - 1)
    listBox1.TopIndex = listBox1.Items.Count - numItems + 1;

很遗憾,ListView 中既不存在 ItemHeight 也不存在 TopIndex。

但主要问题是:我如何告诉虚拟 ListView 它应该显示特定范围的索引 - 或确保显示某些索引?

【问题讨论】:

  • 为什么不使用事件 CollectionChanged?如果要显示新行,则需要通知 listview 数据源更改。
  • @Na Na:不确定如何使用 CollectionChanged,它是 ObservableCollection 的一部分。你能举个例子吗?

标签: c# .net winforms listview


【解决方案1】:

您可以使用属性TopItem 设置ListView 中的顶部可见项(无论是否处于虚拟模式):

listView1.TopItem = listView1.Items[itemIndex];//itemIndex is the index of the item 
//you want to make visible as the top item.

【讨论】:

  • 我认为在虚拟模式下我们无法使用物品。但这工作得很好。谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
相关资源
最近更新 更多