Navigate to View from ViewModel and pass data.

 
60   95.3
Apr 07, 2011
 
Good Evening,

Im wondering if someone could give me some guidance on a Silverlight problem I'm having?

What I am doing now is building a page in Silverlight using the MVVM pattern to retrieve and display my data. My page will be a standard "List" type page that uses the datagrid to display and filter data.

What I would like to do is, If the user clicks an edit button on this list page, I would like to navigate to a details page which will contain a dataform that I want to bind to the same collection of data that is bound to the datagrid of the list page. What I'm thinking has to happen is the List ViewModel has to create the Details Page and set the Details Page datacontext to itself (being the instance of the List ViewModel). I'm not sure how I would create the view, navigate to it and pass the reference to the view model.

You may be wondering why i would want a details page that takes a collection. This is so I can simulate behaviour that my user is familiar with in their current access application. Mostly, it comes in handy when the user filters the list view they are able to enter a page where they could navigate and edit any of the results from their filter without having to open and close the details page repeatedly.

If you have any advise or an alternate pattern to achieve this kind of functionality it would be greatly appreciated.

Regards,



1,128   99.9
Apr 08, 2011
There are probably numerous was to accomplish this, what I usually do (in WPF, I haven't done any SL development) is the following:

1. Click edit button on list page, fires an edit command in ListVM
2. ListVM Edit Command gathers necessary data and creates an EditList event (and adding the data to the event)
3. Trigger the event using Event Aggregator Design Pattern
4. ListView (or someone else) listens to the EditList event, and display a EditView page
5. Populate the EditViewModel with data fetched from the EditList event

Not sure this is best solution, but I found it useful. The Event Aggregator pattern is beneficial when communication between loosly coupled Views and their ViewModels.


Good Luck!

 2 comments
 
Thanks for the answer Robert. I was wondering if you could explain a little more for steps 4 and 5. Do I create an instance of the view in the event receiver on the ListView set the ViewModel properties with the data I require and then call navigate to the URI of the EditView? Its the last part I'm most confused about. If I call Navigate to a URI, will I get the instance I create in event listener. Thanks, --- Chris Nickel  Apr 09, 2011
 
In Wpf, I usually don't create the instance in the event listener nor in the event itself. Rather I pass the necessary data in the event that is needed by the ListView to be able to create the EditView. Hope that helps. --- Robert Blixt  Apr 10, 2011