Implementation Sugguestion

Topics: Prism v4 - WPF 4
Oct 15, 2010 at 2:58 AM

Can anyone suggest the best way to implement what I am trying to accomplish?  I have a Main View which will contain either a List View or an Edit View.  Both the List View and Edit View will contain a Toolbar.  On the List View Toolbar there will be an Edit Button which when clicked should replace the List View with the Edit View.  And the Edit View Toolbar will contain a Search Button which when clicked should replace the Edit View with the List View.  Hopefully, I explained what I am trying to accomplish.

Oct 15, 2010 at 3:14 PM
Edited Oct 15, 2010 at 3:14 PM

Hi,

Assuming that these views could be placed in different modules, a possibility for implementing what you’re explaining would be to have a SingleActiveRegion (for example, a ContentControl) named “MainRegion” in which the views could be inserted, and have the buttons in the toolbar switch between views by publishing an event through the Event Aggregator. If both views were located in the same module, it wouldn’t be necessary to use the Event Aggregator and you could switch between views directly from your ViewModel.

To this purpose, the View Injection approach should be used, and the views should be registered with a name, like this:

regionManager.Regions["MainRegion"].Add(new Views.ListView(),"ListView");

When switching, each view would publish a “SwitchViewEvent” with the other view’s name as a parameter, to indicate that it should be activated.

For example, the code in your button for replacing the List View for the Edit View could be like this (assuming that myListView is a reference to your current view):

eventAggregator.GetEvent<SwitchViewsEvent>().Publish(“EditView”);

and the code in your Edit View for subscribing to that event could be like this:

eventAggregator.GetEvent<SwitchViewsEvent>().Subscribe(addViewtoRegion);

private void addViewtoRegion(string viewName)
{

var view = regionManager.Regions["MainRegion"].GetView(viewName);

if(view !=null)regionManager.Regions["MainRegion"].Activate(view);

}

Since the SingleActiveRegion can only hold one active region at a time, activating the EditView will deactivate the ListView and vice-versa.

You can find more information about this in the UI Composition Quickstart (there is only a Silverlight version, but it could be useful anyway).

Hope this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero

Oct 17, 2010 at 3:27 AM

Thank You,

That does exactly what I was looking for.