In a WPF application using MVVM I query a database to get an ObservableCollection of clients, create an ICollectionView and apply a filter function.
On my usercontrol I bind the text used for the filter to a textbox and the ICollectionView to a Listbox.
The ICollectionView initally contains 1104 clients (just ClientID and ClientName).
Retrieving the data from the database is very quick. However, the listbox takes about 4 seconds to populate.
As I enter text into the filter, if the number of clients to return is low then the listbox redraws relatively quickly. However, if I clear out the textbox then it's another 4 seconds to redraw.
Am I missing something, or is my code not very well written.
Thanks for any advice/help.
View:
<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));
}
}
}
GetClients().ContinueWith(x => { });
. Just don't do that. Read this instead: msdn.microsoft.com/en-us/magazine/dn605875.aspx – ChristianMurschallContinueWith
. – DonBoitnott