TreeMap control primer with WPF MVP-VM

by 21. August 2011 09:34

Source: (2.68 mb)

Note: the sample application uses a Portable Class Library (a library that can be used by WPF, Silverlight, Phone and XBox projects).  Resharper has not been updated to support these libraries so it will underline code in red indicating that it cannot find the applicable module - disregard these visual indications as they are false.

I learned how to use the TreeMap control from Diptimaya Patra's blog "Tree Map Control In Silverlight 3".   You may recognize the results as I used his data structures to complete the application I created to learn how to interact with the TreeMap.

My objective was to generate the TreeMap data from a reusable data structure (so the infrastructure could be easily used by other Presenters/processes).   Since REST services are the popular data sources these days I provide mock data from my data access layer using the following structure:

With the Model-View-Presenter, View-Model (MVP-VM) pattern the Presenter is responsible for instantiating the View (line 22 below), configuring the data context, and wiring up events.   In this case the App.xaml instantiates our Presenter and will Show() the shell that it is returned by Initialize().

With MVP-VM no business logic is in our View, the only code it will have is to raise events so that the Presenter can subscribe to them (line 25 above); this permits the View to remain reusable in other modules.   The DriveView (MainWindow class) is shown below:

If a TreeMap block is clicked on (right or left mouse buttons) or hovered over with the mouse the HandleConcreteTreeMapEvent (instantiated on line 46 of the Presenter above) will handle the request.  This method on the Presenter is called by the baseclass ViewEvent process which uses a factory to determine what ConcreteEvent to pass in to HandleConcreteTreeMapEvent.

The DriveTreeMapEventHandler (on line 49 of the Presenter above) contains the logic that will update the ViewModel - it follows:

We'll want to extend the TreeMapViewModel (baseclass) at some point so we'll derive DriveViewModel from it, however for now its existing functionality is all we need to get our TreeMap bootstrapped so it is currently empty.

The important thing to note in this application is that the View and ViewModel are loosely coupled, they could easily be reused in other application.   


Blog videos and references to CodePlex projects are no longer valid