Having read all the StackOverflow entries regarding Model-View-ViewModel architecture along with most of the readily available resources on the net I have come to the conclusion that it is the de-facto standard for building SOLID Silverlight apps.
I started to plan my next application using this architecture. One of the requirements for the application is to build or change the visual component structure. For example, responding to user interaction I would like to programmatically append an unknown-at-compile-time number of Rectangles and Ellipses to a specific Canvas or Canvases.
I started to twist my mind to apply the MVVM pattern and think where the code responsible for attaching the new components should go. I concluded that it doesn’t belong to any of the MVVM layers and therefore it is impossible to apply this architecture in scenarious where you have to manipulate the component tree at runtime.
Is there a way to still use Model-View-ViewModel in these kind of scenarious or it is only limited to work with a fixed View component structure?
Having read all the StackOverflow entries regarding Model-View-ViewModel architecture along with most of the
Share
Don’t manipulate the component tree. Instead, manipulate a model that represents the component tree. Then have your view bind to the various collections and properties in that model to produce its visuals.
What follows is a really simplified example. It just shows the concepts – please don’t take it as indicative of how you should factor your code.
First, my model:
Next, I expose a collection of said shapes (you would use another model to contain this collection). Then I bind to it in my view: