问题描述
我的窗口中有一个ListView
. ListView
的默认ItemsPanel
被替换为WrapPanel
.我也有一个DataTemplate
,因为它是ListViewItem
.在运行系统中,主窗口将在一段时间内没有响应,因为ListView
具有超过700个(并不断增加)ListViewItem
(来自数据绑定).有没有办法使主窗口保持响应状态?
I have a ListView
in my window. The ListView
's default ItemsPanel
was replaced with WrapPanel
. I also have a DataTemplate
for it's ListViewItem
s. In Runtime, main window will not responding for some time because the ListView
have more than 700 (and keep increasing) ListViewItem
s (from data binding). Is there a way to keep the main window responsive?
可选:当ListView
未准备好时,我希望在ListView
上显示一个文本(或ProgressBar
,如果可能的话),并说请稍候..."或正在加载项目". ..".
OPTIONAL: When the ListView
is not ready, i want a text (or ProgressBar
if possible) show up over the ListView
and saying something like "Please Wait..." or maybe "Loading Items...".
XAML:
<ListView x:Name="MyListView" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" HorizontalAlignment="Left" Height="577" VerticalAlignment="Top" Width="902" ScrollViewer.HorizontalScrollBarVisibility="Auto" Foreground="Black" Margin="10,10,0,0" ScrollViewer.CanContentScroll="True" BorderBrush="#FFC54B4B" BorderThickness="3" Background="White">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel MaxWidth="{Binding (FrameworkElement.ActualWidth),
RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
ItemWidth="{Binding (ListView.View).ItemWidth,
RelativeSource={RelativeSource AncestorType=ListView}}"
MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
ItemHeight="{Binding (ListView.View).ItemHeight,
RelativeSource={RelativeSource AncestorType=ListView}}" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
我尝试过:
List<something> MyList = new List<something>();
ThreadPool.QueueUserWorkItem(_ =>
{
( Create MyList here...)
Dispatcher.BeginInvoke(new Action(() =>
{
MyListView.ItemsSource = MyList;
}));
});
在ListView准备就绪之前,主窗口仍然没有响应.
Main window is still not responding until the ListView ready.
推荐答案
谷歌搜索一段时间后.在这篇文章中,对WrapPanel进行虚拟化并非没有可能!方法如下:
After googling for some time. Thx to this article, Virtualizing WrapPanel is not impossible! Here's how:
- 从此处 下载代码
- 将其添加到您的项目中(在VS2010中:项目">添加现有项")
-
将名称空间添加到您的XAML:
- Download the code from here
- Add it to your project (In VS2010: Project > Add Existing Item)
Add the namespace to your XAML:
xmlns:mwc="clr-namespace:MyWinCollection"
将VirtualizingWrapPanel
用作ListView
的ItemsTemplate
:
Use VirtualizingWrapPanel
as your ListView
's ItemsTemplate
:
<ListView x:Name="MyListView" ItemsSource="{StaticResource ItemCollection}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingWrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
完成!
由于并非所有项目都能一次渲染,因此主窗口现在可以完全响应.
Since not all items are rendered at once, the main window is fully responsive now.
希望这项帮助! :D
Hopes this help! :D
顺便说一句,伙计们!你们救了我的一天.
Btw, thx guys! You guys saved my day.
这篇关于将WrapPanel虚拟化为ListView的ItemsTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!