您可以使用标准ListBox通过数据绑定“延迟加载”您的项目.这里的关键字是“数据虚拟化”.您必须将IList实现到您想要数据绑定的类.仅对当前可见的项目和下一个计算的~2个屏幕调用索引器方法.这也是您应该为项目布局使用固定大小网格的原因,而不是基于所有包含项目的计算高度的堆栈面板(性能!).
您不必实现所有IList成员,只需几个.这是一个例子:
public class MyVirtualList : IList { private List<string> tmpList; public MyVirtualList(List<string> mydata) { tmpList = new List<string>(); if (mydata == null || mydata.Count <= 0) return; foreach (var item in mydata) tmpList.Add(item); } public int Count { get { return tmpList != null ? tmpList.Count : 0; } } public object this[int index] { get { Debug.WriteLine("Just requested item #" + index); return tmpList[index]; } set { throw new NotImplementedException(); } } public int IndexOf(object value) { return tmpList.IndexOf(value as string); } public int Add(object value) { tmpList.Add(value as string); return Count - 1; } #region not implemented methods public void Clear() { throw new NotImplementedException(); } public bool Contains(object value) { throw new NotImplementedException(); } public void Insert(int index,object value) { throw new NotImplementedException(); } public bool IsFixedSize { get { throw new NotImplementedException(); } } public bool IsReadOnly { get { throw new NotImplementedException(); } } public void Remove(object value) { throw new NotImplementedException(); } public void RemoveAt(int index) { throw new NotImplementedException(); } public void copyTo(Array array,int index) { throw new NotImplementedException(); } public bool IsSynchronized { get { throw new NotImplementedException(); } } public object SyncRoot { get { throw new NotImplementedException(); } } public IEnumerator GetEnumerator() { throw new NotImplementedException(); } #endregion }
在调试时,您可以看到并非所有项目都是一次加载,而是仅在需要时加载(请参阅Debug.WriteLine()).
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。