c# - 将 WrapPanel 虚拟化为 ListView 的 ItemsTemplate

标签 c# wpf listview

我的窗口中有一个 ListViewListView 的默认 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/

相关文章:

c# - 通过 WCF 服务公开 Entity Framework

c# - Windows 应用商店应用程序中的 IObservableVector<T> 不能用作数据绑定(bind)源

wpf - WPF RichTextBox 是否支持波浪下划线和内联图像?

c# - 在 WPF 应用程序中读取 Xbox Controller

java - 大 Nerd 牧场 Android。 RecyclerView不会显示数据

javascript - Jquery this.find() 没有找到任何东西?

Android,如何在按下 "Back"按钮关闭searchView时关闭ListView

javascript - 想要在开始日期文本框中添加迄今减去一天的一年,并将其返回到结束日期文本框中

c# - 在 C# 中通过 WCF 访问需要证书的 Web 服务 (HTTPS)

c# - 如何将依赖注入(inject)与 Facade 模式一起使用?