将WrapPanel虚拟化为ListView的ItemsTemplate

Wah*_*hyu 5 c# wpf listview

ListView在窗户上。该ListView的默认ItemsPanel被替换WrapPanel。我也有DataTemplate它的ListViewItem秒。在运行系统中,主窗口将在一段时间内不响应,因为它ListView具有700个(并不断增加)ListViewItem(来自数据绑定)。有没有办法使主窗口保持响应?

可选: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>
Run Code Online (Sandbox Code Playgroud)

编辑:

我尝试了这个:

List<something> MyList = new List<something>(); 

ThreadPool.QueueUserWorkItem(_ =>
      {

          ( Create MyList here...)

          Dispatcher.BeginInvoke(new Action(() =>
          {
              MyListView.ItemsSource = MyList;
          }));
      });
Run Code Online (Sandbox Code Playgroud)

直到ListView准备就绪,主窗口仍然没有响应。

Evk*_*Evk 5

您使用的面板(WrapPanel)无法进行UI虚拟化(与默认ListView ItemPanel模板中使用的VirtualizingStackPanel不同)。这意味着您的所有项目均已渲染,即使当前不可见。据我所知,WPF没有内置的虚拟化包装面板,因此您可以尝试一些免费的虚拟化包装面板(例如-http://virtualwrappanel.codeplex.com/),但是我不能说它们有多好,我使用不是免费的Telerik版本。另一个选项是切换回VirtualizingStackPanel。除此之外,请确保您将项目加载到非UI线程上(如另一个答案所述)。


Wah*_*hyu 3

经过谷歌搜索一段时间后。感谢这篇文章,虚拟化 WrapPanel 并非不可能!就是这样:

  1. 从这里下载代码
  2. 将其添加到您的项目中(在 VS2010 中:项目 > 添加现有项目)
  3. 将命名空间添加到您的 XAML:

    xmlns:mwc="clr-namespace:MyWinCollection"
    
    Run Code Online (Sandbox Code Playgroud)
  4. 用作VirtualizingWrapPanelListViewItemsTemplate

    <ListView x:Name="MyListView" ItemsSource="{StaticResource ItemCollection}">
      <ListView.ItemsPanel>
         <ItemsPanelTemplate>
    
            <VirtualizingWrapPanel Orientation="Horizontal"/> 
    
         </ItemsPanelTemplate>
       </ListView.ItemsPanel> 
    </ListView>
    
    Run Code Online (Sandbox Code Playgroud)

完毕!

由于并非所有项目都会立即呈现,因此主窗口现在完全响应。

希望这有帮助!:D

顺便说一句,谢谢大家!你们救了我的命。