0

I am trying to get my head around MVVM. For a simple list->data view it's no problem. But I am struggling to understand how multiple layers work. I sort of have something working but it's very hit and miss as to which bits work and which bits don't. For example, some data updates, some doesn't. Anything in a deeper level which should affect a list at an upper level sometimes updates the list, sometimes doesn't. There must be a pattern but I have yet to spot it. Does anybody know of any tutorials with more than just a list->data type of view?

Example:

List of widgets
+- Widget name
+- Widget description
+- List of Widget parts
   +- Part ID
   +- Colour

In that example I could have a three-column approach - list of widgets in the left, widget information in the middle including the parts list, and then the part detail on the right.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
GeoffM
  • 1,603
  • 5
  • 22
  • 34
  • 3
    Not enough to go by, also this place is **not** for book/tutorial/etc suggestions or link requests. – H.B. Jul 16 '12 at 17:03
  • 1
    Are you using any frameworks such as Prism, Caliburn Micro, etc? – myermian Jul 16 '12 at 17:03
  • 2
    This is not a direct answer to your question, but more of a side note, you might want to look at "Event Aggregation" patterns. This is a good way for disconnected parts of an MVVM application to communicate with each other. – CodingGorilla Jul 16 '12 at 17:06
  • m-y - I'm not but maybe that's what I need to be looking at. CG - I'll have a look. Thanks, you two - it's given me pointers of what I should be looking at. – GeoffM Jul 16 '12 at 17:17
  • @GeoffM: Look at this StackOverflow question (http://stackoverflow.com/questions/1409553/what-framework-for-mvvm-should-i-use) to decide which framework to use... They're all good overall, some offer a more narrow scope of features, while others are more broad. Personally I chose to learn Prism because it is, in my opinion, ideal for modularized/component designs. I do warn you though, Prism has a **very** steep learning curve. – myermian Jul 16 '12 at 17:27

1 Answers1

3

You should have multiple ViewModels, one for each level. Then you can provide events to let the upper levels update on change.

For example you can have a

public class WidgetListViewModel 
{
    public ObservableCollection<WidgetViewModel> Widgets {get; set; } 
}

public class WidgetViewModel
{
    public string WidgetName { get; set; }
    public string WidgetDescription { get; set; }
    public ObservableCollection<WidgetPartViewModel> Parts { get; set; }
}

public class WidgetPartViewModel
{
    public int PartId { get; set; }
    public System.Windows.Media.Color Color { get; set; }
}

Having events (including a simple pattern) is described here Events in .Net

Furthermore, I recommend watching this excellent video tutorial on MVVM: Jason Dollinger on MVVM

The video covers some issues of Unity also! (which could be very valuable for you)

The sourcecode he developes is also available: Lab49 Sourcecode by Jason Dollinger

sellmeadog
  • 7,437
  • 1
  • 31
  • 45
Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
  • The multiple view models question was nagging in my brain. Time for more reading and watching! Thanks. – GeoffM Jul 16 '12 at 17:43