I have simple test (WPF – MVVM) client (for WCF oData service) app:
ViewModel
public class MainViewModel : ViewModelBase
{
private MyEntities context;
public ICollectionView Collection { get; private set; }
private string searchString = "";
public string SearchString
{
get { return searchString; }
set
{
searchString = value;
Collection.Refresh();
}
}
public MainViewModel()
{
context = new MyEntities(new Uri("http://localhost:3780/Live.svc"));
Collection = new CollectionView(context.Clients);
//Collection = new CollectionView(context.Clients.ToArray());
Collection.Filter = (o) => (o as Client).FullName.ToString().StartsWith(SearchString);
}
}
and View
<ListBox ItemsSource="{Binding Collection}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FullName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox Grid.Row="1" Text="{Binding SearchString, UpdateSourceTrigger=PropertyChanged}" />
I have two questions:
- Why the movement of the ListBox slider slows down S_O___M_U_C_H (I
have only 40 clients in the collection)? -
Why, if I change this
Collection = new CollectionView(context.Clients);to this
Collection = new CollectionView(context.Clients.ToArray());so to break “connection” with the context through IEnumerable shell
(it helps with UI performance problems) – stops working filtering! I
do not understand why the disconnection of the collection from data
context damage the filtering…
So, my final question is – is it possible to avoid GUI performance problems and at the same time implement filtering? And if so, how?
Any suggestions are welcome!
Ok, guys, I solved my problem. I should just pay attention to the message In the VS Output window:
Using CollectionView directly is not fully supported. The basic features work, although with some inefficiencies, but advanced features may encounter known bugs. Consider using a derived class to avoid these problems.
The solution was simple:
I changed this
to this
and then there are no GUI performance problems and filtering works as expected!
The result code was:
ViewModel:
and View:
WCF Data Service: