Quick question about change notification

Oct 3, 2012 at 9:59 PM

Overall, I am very happy with Simple MVVM Toolkit. It seems to meet all of my needs for WPF without uneccesary complexity.

I created a simple example based on your WPF project and added a few things to ensure this project would meet my needs. I was hoping to use an EF code first project for the model, update data, have a viewmodel that combines several objects, and drop down lists to update from an enum. Everything seems to work great and overall it is much simpler and more intuitive than any of the other frameworks I tried. The only thing that would have made this easier would have been a few more WPF examples.

I primarily used the base WPF project and merged that with the property association and enum examples. One problem I ran into was that the view was not updating until I added a NotifyPropertyChanged event for classes used to compose the viewmodel. I don't quite understand how this worked in the property association example and right now I have just decided it may just be a Silverlight vs. WPF difference.

Coordinator
Oct 15, 2012 at 2:11 PM

When I first created the toolkit, Silverlight was on the upswing, so almost all the samples are in Silverlight.  However, I don't think WPF would behave any differently than Silverilght with respect to property associations.  To see how this works, check out the Programming Reference, where an example is given.

What's happening here is that the NotifyPropertyChanged event for the Customer model fires the event for the CustomerViewModel.  But you have to set up the associations.  I would suggest putting a breakpoint on the code where you are calling AssociateProperties and make sure that is getting hit.  It also may be a problem in your WPF data bindings.  It's the data binding that will call the property getter in ViewModel when the NotifyPropertyChanged event is fired.

Cheers,
Tony 

Oct 15, 2012 at 4:14 PM

Thank you for explaining the way this works and pointing me to the reference. I had read about the AssociateProperties example in the reference and that is what I based my code on. I didn't actually assocate any properties because I was actually after items contained in an IList on the Model. That IList contains a class that has an integer value that is converted to a decimal value in the view. The view contains 30 of these decimal values. Generally, 13-27 of these numeric values will be set on the view and stored in the Model's Ilist.  I couldn't decide whether to associate those 30 items with the Ilist or not but it sounds like that is what I should have done. I am a total beginner with WPF but I am close to having enough of an understanding to finish up this first application. You response about the model dialog was perfectly timed since that is the next major piece of funtionality I will be tackling.

Here are some pieces from my application that may show what I am trying to accomplish. It seems to be working right now but I am still at a near prototype stage. I appreciate any help even if it is just pointing out part of a manual or a book that I should read.

From the Model:

public virtual IList<Item> items
{
    get
    {
        return items;
    }

    private set
    {
        this.items = value;
        NotifyPropertyChanged(m => m.Items);
    }
}

From ViewModel: 

public Criterion Criterion
{
    get
    {
        if (null == base.Model)
        {
            this.Criterion = serviceAgent.CreateCriterion();
        }

        return base.Model;
    }
    set
    {
        base.Model = value;
        // Propogate increment size to view increment size if it is useable
        if (null != base.Model && 0.0m != base.Model.ItemSizeIncrement)
        {
            this.ItemSizeIncrement = base.Model.ItemSizeIncrement;
        }
        NotifyPropertyChanged(m => m.Criterion);
    }
}

public decimal[] Items
{
    get
    {
        if (null == items)
        {
            items = new decimal[30];
            if (null != this.Criterion && this.Criterion.Items.Count > 0)
            {
                for (int i = 0; i < this.Criterion.Items.Count; i++)
                {
                    items[i] = this.Criterion.ItemSizeIncrement * this.Criterion.Items[i].Size;
                }
            }
        }

        return items;
    }

    set
    {
        items = value;
        NotifyPropertyChanged(m => m.Items);
    }
}