我的窗口中有一个 ListView
。 ListView
的默认 ItemsPanel
已替换为 WrapPanel
。我还有一个 DataTemplate
,因为它是 ListViewItem
。在运行时,主窗口将有一段时间没有响应,因为 ListView
有超过 700 个(并且还在不断增加)ListViewItem
(来自数据绑定(bind))。有没有办法让主窗口保持响应?
可选:当 ListView
未准备好时,我希望文本(或 ProgressBar
如果可能)显示在 ListView
上并说诸如“请稍候...”或“正在加载项目...”之类的内容。
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 准备好之前,主窗口仍然没有响应。
最佳答案
您使用无法进行 UI 虚拟化的面板 (WrapPanel)(与默认 ListView ItemPanel 模板中使用的 VirtualizingStackPanel 不同)。这意味着您的所有项目都会被渲染,即使是那些目前不可见的项目。据我所知,WPF 没有内置虚拟化包装面板,因此您可以尝试一些免费的虚拟化包装面板(例如 - http://virtualwrappanel.codeplex.com/ ),但我不能说它们有多好,我使用的是 Telerik 的版本不是免费的。另一种选择是切换回 VirtualizingStackPanel。除此之外,确保在非 UI 线程上加载项目(如另一个答案中所述)。
关于c# - 将 WrapPanel 虚拟化为 ListView 的 ItemsTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32720694/