- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在使用 MVVM 的 WPF 应用程序中,我查询数据库以获取客户端的 ObservableCollection,创建 ICollectionView 并应用过滤器函数。
在我的用户控件上,我将用于过滤器的文本绑定(bind)到一个文本框,并将 ICollectionView 绑定(bind)到一个列表框。
ICollectionView 最初包含 1104 个客户端(仅 ClientID 和 ClientName)。
从数据库中检索数据非常快。但是,列表框需要大约 4 秒的时间来填充。
当我在过滤器中输入文本时,如果返回的客户数量很少,那么列表框会相对快速地重新绘制。但是,如果我清除文本框,则需要 4 秒才能重新绘制。
我是不是遗漏了什么,或者我的代码写得不是很好。
感谢您的任何建议/帮助。
查看:
<UserControl x:Class="ClientReports.Module.SchemeSelection.Views.Clients"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ClientReports.Module.SchemeSelection.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True" >
<Grid>
<StackPanel>
<TextBox materialDesign:HintAssist.Hint="Client Search"
Style="{StaticResource MaterialDesignFloatingHintTextBox}"
Text="{Binding Search, UpdateSourceTrigger=PropertyChanged}"/>
<ListBox ItemsSource="{Binding ClientsFiltered}" DisplayMemberPath="ClientName" />
</StackPanel>
</Grid>
</UserControl>
View 模型:
using ClientReports.Common.Infrastructure.Models;
using ClientReports.Common.Infrastructure.Services;
using Prism.Mvvm;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Data;
namespace ClientReports.Module.SchemeSelection.ViewModels
{
public class ClientsViewModel : BindableBase
{
private IClientService clientService;
public ClientsViewModel(){ }
public ClientsViewModel(IClientService clientService)
{
this.clientService = clientService;
Clients = new ObservableCollection<Client>();
GetClients().ContinueWith(x => { });
}
public ObservableCollection<Client> Clients { get; }
public ICollectionView ClientsFiltered { get; set; }
private string clientFilter;
public string Search
{
get => clientFilter;
set
{
clientFilter = value;
ClientsFiltered.Refresh();
RaisePropertyChanged("ClientsFiltered");
}
}
private bool Filter(Client client)
{
return Search == null
|| client.ClientName.IndexOf(Search, StringComparison.OrdinalIgnoreCase) != -1;
}
private async Task GetClients()
{
var clients = await clientService.GetAllAsync();
foreach (var client in clients)
{
Clients.Add(client);
}
ClientsFiltered = CollectionViewSource.GetDefaultView(Clients);
ClientsFiltered.Filter = new Predicate<object>(c => Filter(c as Client));
}
}
}
最佳答案
ListBox 可能需要 4 秒来填充,因为未启用虚拟化,因此 WPF 必须创建 1104 个 ListBoxItem(并在清除过滤器时重新创建它们)。默认情况下,为 ListBox 启用虚拟化,但您有时甚至可能在没有意识到的情况下禁用它。在您的示例中,您的 ListBox 位于垂直的 StackPanel 中,这可能是导致此行为的原因。您可以尝试通过以下方式重写 XAML:
<UserControl x:Class="ClientReports.Module.SchemeSelection.Views.Clients"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ClientReports.Module.SchemeSelection.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox materialDesign:HintAssist.Hint="Client Search"
Style="{StaticResource MaterialDesignFloatingHintTextBox}"
Text="{Binding Search, UpdateSourceTrigger=PropertyChanged}"
Grid.Row="0"/>
<ListBox ItemsSource="{Binding ClientsFiltered}"
DisplayMemberPath="ClientName"
Grid.Row="1" />
</Grid>
</UserControl>
如果没有帮助,您可以尝试为您的 ListBox 设置固定高度并再次检查。
如果两者都没有帮助,请查看有关虚拟化的 Microsoft 文档以了解其他可能的原因:https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/optimizing-performance-controls
关于c# - 过滤 ICollectionView<object> 时性能下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52699681/
我不明白为什么我的 ICollectionView 没有刷新。谁能解释我做错了什么? 我做了这样一个 View 模型: class ViewModel : INotifyPropertyChanged
有什么办法吗 ICollectionView.Refresh() 或者 CollectionViewSource.GetDefaultView(args.NewValue).Refresh(); 在
我已经编写了用于在 ComboBox 中过滤项目的代码: 我的问题是,你会怎么做? 我认为这个带有反射的解决方案可能会非常慢.. ICollectionView view = CollectionVi
我有一个 ObservableCollection : public ObservableCollection Collection { get; set; } = new ObservableCol
ImageAlbums是一个 ICollectionView类型和 GlobalCollection.MyImageAlubms是一个 ObservableCollection类型。 ImageAlb
也许有人可以帮助我?我有以下场景: 一个简单的 View : Refresh 背后的代码: publ
我有 3 个级联 ICollectionView s 在我的 View 模型中,其中一个依赖于另一个。 第一个不绑定(bind)到 View 上的控件,而是用作两个即将出现的作为主细节显示给用户的键过
我正在尝试在 WPF 中实现 MVVM 模式。我关注了 Jeremy Alles 的 Very simple MVVM demo application 。我有一个 ListBox,它绑定(bind)
我在我的 View 模型中将一个 ListView 绑定(bind)到一个 ICollectionView。 ICollectionView 有一些预定义的过滤器,当您单击某些按钮时会应用这些过滤器。
我有一个类 Class ABC() { public string Name{get;set;} public string Category{get;set;} } 由此类形成的列表
我创建了一个小型用户控件,其中包含一个 ICollectionView 和一个 ObservableCollection。 ObservableCollection messages; public
我有一个名为 ItemType 的对象的集合,每个集合都有一个 Item 的子集合。顶级集合被包装到一个 ObservableCollection 中,因此它会在用户从集合中添加或删除内容时做出响应。
我有一个使用 Entity Framework 查询数据库并将结果放在 ICollectionView 中的方法。 ICollectionView 充当 DataGrid 的 ItemsSource。
有什么方法可以自动更新 ICollectionView 上的过滤器,而无需在进行相关更改时调用 Refresh()? 我有以下内容: [Notify] public ICollectionView W
如何在 C# 中从 ICollectionView 中删除项目? Dictionary entityDict; public ICollectionView DictView { get; set;
我有一个 .Net 4.5 应用程序正在迁移到基于 WPF 的 RxUI(在撰写本文时保持最新,6.0.3)。我有一个文本字段,它应该用作具有相当常见的 throttle 等的过滤器字段,这些东西首先
所以我有一个数据网格,我需要为其添加自定义排序,我还需要知道排序的确切顺序。 我已阅读,为了做到这一点,我需要实现自定义 icollectionview 并将其绑定(bind)到数据网格。 我遇到的问
我有 ICollectionView 看起来像 public ICollectionView UsersCollectionView { get { var view
我将 ObservableCollection 绑定(bind)到 dataGrid,现在我想过滤显示的数据 我发现我需要使用 ICollectionView 但我不确定如何使用我的 MVVM 模式添
我是WPF的新手,看起来我也不完全理解C#。 下面的代码应该向 DataGrid 提供排序后的数据。 这是我难以理解的代码: ObservableCollection PersonsCollectio
我是一名优秀的程序员,十分优秀!