在使用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>
ViewModel:
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 ListBoxItems(并在清除过滤器后重新创建它们)。默认情况下,为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://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/optimizing-performance-controls
关于c# - 筛选ICollectionView <object>时性能降低,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52699681/