views:

182

answers:

3

Hi, I have a list page which list records in graphical representation. On clicking a some graphical portion or some button-which is added runtime on page- i want to activate new page by passing a some data as a parameter.

Eg. I have Category list page, the UI representation is done at runtime. So clicking on some category I want to list its related Product on another page, I am getting its-CategoryID,

Now, my problem is activating/displaying new page in MVVM patern in WPF.

Just like Response.Redirect in ASP.Net

Thanks in advance for your valuable suggestion.

+2  A: 

This isn't really an "MVVM thing" it's more a question of what activates your screens. You might want some overarching service that either maintains your application state, or handles displaying of views (and possibly wiring up of ViewModels too), then in your show details command you could simply do something like:

MyAppService.ShowDetails(currentID);

You may want to take a look at the PRISM Region system, and the reference PRISM Stock application for some architecture examples. A lot depends on how your UI is structured, but hopefully it will at least give you some ideas!

Steven Robbins
Yaa, But in MVVM we need to follow the standard, otherwise we can create an Instance of new Window and just Call its Show() method.But in MVVM there is some Workspaces-Command-PropertyChanged mechanism. So, how to accomplish using this standard?
Naresh Goradara
MVVM is about separating your presentation from your data, whist proving rich binding, it's not a "super pattern" that covers your whole application architecture! You use it in *addition to* other architecture patterns. As far as MVVM goes you would get the request to view details via Commanding, but it's not your VMs responsibility to know how to load another screen!
Steven Robbins
A: 

There are a couple of ways to do this in MVVM. I would agree with Steve Robbins that "it's not your VM's responsibility to know how to load another screen", but it is your VM's responsibility to receive notification from the view that something has occured that requires display of a new screen, and to pass that notification along to the object that will do the loading and unloading of views as they are needed. The point to remember is that each VM is bound to a view, and the VM gets replaced when a new view is displayed. So, something else, in the nature of an executive, needs to do the replacing.

That's why Composite WPF (Prism) framework is getting so much attention. I struggled with the same issues that you have raised before learning Prism, which provides the executive. I assume your Category list is being displayed in an items control. Basically, in your situation, I would probably bind the SelectedItem property of the control to a SelectedItem property in the VM. I would have the setter of that property invoke an MVVM method that notifies the Prism framework of the selection, and that passes along the SelectedItem that was passed to the property setter. I would use Prism to load the module with the page that needs to be displayed.

The nice thing about Prism is that it will work if you want your Product page to replace your Category page, and it will work just as well if you want to show a Product pane next to a Category pane in the same window. It's a simple matter of specifying one region or two when you set up your Shell (the main window).

There is a learning curve to Prism, but it isn't very steep. It works very well with MVVM, and it is a great framework for any application that can benefit from partitioning. I consider it to be a key element of WPF application design.

David Veeneman
A: 

I was thinking to handle this using code behind for navigation and commands for data manipulation, the difference being this: if it touches the database and it must be testable,than it must be an ICommand in a VM, if it is navigation it must be code behind. I understand that the app design should try not to mix operations and this is not always feasible.

mico