在使用 MVVM 的 WPF 应用程序中,我查询数据库以获取客户端的 ObservableCollection,创建 ICollectionView 并应用过滤器函数。
在我的用户控件上,我将用于过滤器的文本绑定到一个文本框,将 ICollectionView 绑定到一个列表框。
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>
Run Code Online (Sandbox Code Playgroud)
视图模型:
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 …
Run Code Online (Sandbox Code Playgroud)