This is a mirror of official site: http://jasper-net.blogspot.com/

Common problems with Notifications from a View Model

| Friday, April 8, 2011
In the MVVM (Model, View, ViewModel) pattern in xaml based applications (Windows Phone 7, Silverlight and WPF) there are two different ways a view model will notify the view of changes. If you're unaware of the differences you can end up wondering why your changes aren't being reflected in your view.

The most common is implementing the INotifyPropertyChanged interface, the other is through the INotifyCollectionChanged interface. Most developers really won't use second interface but its useful to know about and how it affects your view model.

When you bind to a property on your view model the binding infrastructure will listen for INotifyPropertyChanged events from your view model for that property.

If the type of the property implements INotifyCollectionChanged (such as ObservableCollection) it will also listen for events off that property.

The distinction is important because a common pattern with developers is implement collection properties as automatic properties. This however limits the way you can interact with the collection such that changes will be reflected in the view.

public class CorrectViewModel : ViewModelBase
{
    public CorrectViewModel()
    {
        Items = new ObservableCollection<Item>();??
    }
 
    public ObservableCollection<Item> Items
    {
        get; set;
    }
 
    public void Add()
    {
        var items = GetItems();
 
        Items.Clear(); // Collection changed raised
 
        foreach(var item in items)
        {
            Items.Add(item); // Collection changed raised
        }
    }
 
    private IEnumerable<Item> GetItems()
    {
        return from i in Enumerable.Range(0, 10)
                select new Item
                {
                    Name = "Item: " + i
                };
    }
}
 
public class IncorrectViewModel : ViewModelBase
{
    public IncorrectViewModel()
    {
        Items = new ObservableCollection<Item>();
    }
 
    public ObservableCollection<Item> Items
    {
        get;
        set;
    }

Posted via email from Jasper-net

0 comments: