将WrapPanel虚拟化为ListView的ItemsTemplate
我的窗口中有一个ListView
。 ListView
的默认ItemsPanel
已替换为WrapPanel
。 我也有一个DataTemplate
用于它的ListViewItem
。 在运行时,主窗口将不会响应一段时间,因为ListView
具有超过700(并且不断增加) ListViewItem
(来自数据绑定)。 有没有办法让主窗口保持响应?
可选:当ListView
没有准备好时,我想要一个文本(或可能的ProgressBar
)显示在ListView
并说出“Please Wait …”或者“Loading Items …”之类的内容。
XAML:
编辑:
我试过这个:
List MyList = new List(); ThreadPool.QueueUserWorkItem(_ => { ( Create MyList here...) Dispatcher.BeginInvoke(new Action(() => { MyListView.ItemsSource = MyList; })); });
在ListView准备好之前,主窗口仍然没有响应。
您使用无法进行UI虚拟化的面板(WrapPanel)(与默认ListView ItemPanel模板中使用的VirtualizingStackPanel不同)。 这意味着你的所有物品都被渲染,即使是那些目前看不到的物品。 据我所知,WPF没有内置的虚拟化包装面板,因此您可以尝试一些免费的虚拟化包装面板(例如http://virtualwrappanel.codeplex.com/ ),但我不能说它们有多好,我使用Telerik的版本,这是免费的。 另一个选项是切换回VirtualizingStackPanel。 除此之外,请确保在非UI线程上加载项目(如另一个答案中所述)。
如果UI线程正在执行长时操作,则它将无法处理UI请求。 这也称为无响应。 像这样使用ThreadPool
:
private void operation_Click(object sender, RoutedEventArgs e) { ThreadPool.QueueUserWorkItem(_ => { var result = LongTimeOperation();//set the operation in another thread so that the UI thread is kept responding //use the Dispatcher to "return" to the UI thread Dispatcher.BeginInvoke(new Action(() => { //Use result })); }); }
谷歌搜索一段时间后。 对本文来说,Virtualizing WrapPanel并非不可能! 这是如何做:
- 从这里下载代码
- 将其添加到项目中(在VS2010中:项目>添加现有项目)
-
将命名空间添加到XAML:
xmlns:mwc="clr-namespace:MyWinCollection"
-
使用
VirtualizingWrapPanel
作为ListView
的ItemsTemplate
:
完成!
由于并非所有项目都是一次渲染,因此主窗口现在完全响应。
希望对你有所帮助! :d
顺便问一下,伙计们! 你们救了我的一天。