tags:

views:

141

answers:

2

I will be starting a new project soon and am looking for some architectural advice from those of you who have experience with WPF, Prism, and MVVM.

The project will definitely be WPF and I will be implementing MVVM (I will likely use Josh Smith's MVVM Foundation as a starting point) in order to be able to benefit from the separation of UI/logic etc. I am not sure though if I would benefit from using Prism as well to structure my project.

Let me briefly describe the project. There will be a main "toolbar" that will display a number of widgets. Each widget displays some basic data related to its function and clicking the widget will open a new window that will display much more detailed data and contain a rich UI for viewing/editing the data.

Now, I was thinking that I can use Prism to frame the project but I have never used it before and am not sure if it is suitable for what I am trying to achieve. For example, would my "toolbar" be a shell that contains regions that each widget would populate? Would each new window that is displayed when a widget is clicked also be its own shell with its own region setup? If I can get the pattern down for the toolbar and one widget on the toolbar, I can replicate it for the rest of the widgets.

Aside from Prism, I have a question about how MVVM should be implemented for certain data editing windows. Let's say I have a chart that displays some data and the user is able to directly click/mouse move on the chart to edit the data that he sees. All of the data is in the model and the view model is making that data available to the view via binding. My question is where will the mouse click/move events, that are specific to the chart in that view, be written? We don't want much/anything in the view's code behind and we don't want to have UI event handlers in the view model so I am not sure how this type of scenario is handled. I know that commands are the likely answer here but the MVVM samples I have seen usually show sample commands for simple button clicks. Is the general idea the same?

So, if anyone has any suggestions on the above or any general tips on working with WPF and MVVM/Prism, please let me know.

Thank you.

+1  A: 

There are a few questions in there so I will do my best at covering them all.

I worked on a project that had WPF, MVVM, and Prism along side other frameworks. The best advice is to understand the power and functionality of each before glueing it all together. You don't have to use all the features of Prism for it to be useful in this situation.

For Prism you can use...

  1. Shell and bootstrapper to initialise the application and load modules from other assemblies.
  2. Create and configure Unity for Dependency Injection. You can use other DI Containers. Here you can add global services each module will use.
  3. Use of EventAggregator to notify differnent parts of the application, usually across modules and views
  4. Regions for naming areas on the UI so modules can add a view to a particular location.

The above 4 don't all have to be used but can easily be integrated in a MVVM /WPF application.

For example, would my "toolbar" be a shell that contains regions that each widget would populate?

Here you can have a region you create (you can derive from Region) that will manage the buttons on the toolbar. (I have used a region with regards to a Ribbon). A service can be exposed via an interface that each module can supply the command/image (what ever you have) that when it is clicked will create a ViewModel. You can do this inside the module's Initialisation.

Would each new window that is displayed when a widget is clicked also be its own shell with its own region setup?

If each button opens a brand new window I would suggest introducing a common controller class that will create a generic use window and attach a view model that your module creates. No real need to use regions in this case unless you are gluing different views to a application window that stays open longer than the life of the view itself. The window in basic form can simply be this...

<Window ...>
  <ContentControl Content="{Binding}" />
</Window> 

Where within your controller it can do this...

public void DisplayView(ViewModel vm)
{
   var window = new MyWindow { DataContext = vm };
   window.Show();
}

The controller can be used within your module directly of wrapped within a service... although for testabilty a service and interface would be best. Make sure you have merged your module resources with the Applicaiton.Resources and use DataTemplate's to link your view to the view model.

My question is where will the mouse click/move events, that are specific to the chart in that view, be written?

Don't be afraid of code behind but you can in this case use EventToCommand attached behaviour that will route to a command on your viewmodel. MVVMLight toolkit has this which you can reuse if you want.

DI is very powerful and I encourage using it even without Prism as constructing your view models will be easier.

HTH

aqwert
Thank you for the info and tips. They are greatly appreciated.
Flack
A: 

I think Prism will work great for you.

->would my "toolbar" be a shell that contains regions that each widget would populate?

  1. Put a single region with an ItemsControl in the Shell
  2. Create modules for each widget
  3. Keep adding the widget modules to the same itemscontrol shell region.

The biggest advantage with this is that if you add more modules you don't need to change anything.

->Would each new window that is displayed when a widget is clicked also be its own shell with its own region setup?

No, you can use a 'WindowRegionAdapter' in the shell to create views for your widgets in separate windows.

->where will the mouse click/move events, that are specific to the chart in that view, be written?

You can use attached behaviors to bind events in your view to commands in the ViewModel purely in XAML. Google 'Blend behaviors' or 'attached bahaviors' for how you could go about doing it. There is no need to write any code behind for this.

To be honest I am only trying to give you the keywords you'd want to search to get all the information you need.

NVM
Thanks. I will search some more for additional info.
Flack