Friday 21 September 2012

MVVM: Binding a Linq Query result to a DataGrids’ ItemSource doesn’t update the UI

While creating Desktop Applications using WPF, I always employ the MVVM model if I can. This model is not only great at developing testable n-tier applications, but of course is also highly recommended by Microsoft.

However, as with most software development, there are nearly always specific methods to employ for certain processes and functions.

One of these is while Binding Linq Queries, through a bound property in your ViewModel.

Linq queries as standard return an IEnumerable type. This is fine for the most part, and fits nicely with MVVM, allowing Where statements etc.

However, if you then bind this IEnumerable or List type to a DataGrid, through a property in your ViewModel, and try adding and removing items using the .Add and .Remove extension methods, it becomes apparent that the UI isn’t updating.

This is because the Dot Net Framework has performed certain optimisations, which unfortunately cause it to miss the Add and Remove changes when dealing with IEnumerable and List type objects.

One way around this is to completely break the MVVM model and call the Items.Refresh method on the UI DataGrid. However, this is very very bad practise, and circumnavigates the entire purpose for using MVVM in the first place.

The correct solution is to replace the IEnumerable(of T) or List(of T) with an ObservableCollection(of T).

The Observable collection will automatically notify the UI through the ViewModel, whenever an Add or Remove is called on the collection.

Do remember to import the System.Collections.ObjectModel namespace of course!

No comments:

Post a Comment