Substitute view of module A with view of module B within a single region

Nov 26, 2008 at 9:30 PM
Hi all.

It may sound stupid but imagine this use case:

I have defined a single region

<

 

ItemsControl cal:RegionManager.RegionName="{x:Static inf:RegionNames.DetailsRegion}" />

When I click a particular button within the shell I want to substitute the currently displayed view (of Module A, e.g. a list of customers) by another view that belongs to Module B (e.g. a list of suppliers).
Is it possible to do this or do I have to use for example a TabControl an distribute the views of the different modules to the single tabs?

Thanks a lot for your help!

Nov 27, 2008 at 1:20 PM

Hi

 

Yes, it is possible. You could use a handler for your command like the following one:

Note: If you are showing only one view per time in your DetailsRegion, consider using a ContentControl instead of the ItemsControl. This control allows a maximum of one active view at a time.

 

private void OnChangeView(object obj)

{

IRegion detailsRegion = this.regionManager.Regions[RegionNames.DetailsRegion];

List<object> activeViews = new List<object>(detailsRegion.ActiveViews);

 

// Remove other views showed in the region.

foreach (object view in activeViews)

{

detailsRegion.Remove(view);

// If your region is a ContentControl you can use the Deactivate method instead of removing the view.

// mainRegion.Deactivate(view);

}

 

// Add the view from Module B to the region if it is not present on it.

if (!detailsRegion.Views.Contains(_viewModuleB))

{

detailsRegion.Add(_viewModuleB);

}

 

detailsRegion.Activate(_viewModuleB);

}

 

Please let me know if this helps.

 

Mariano Converti

http://blogs.southworks.net/mconverti
Nov 28, 2008 at 7:01 AM

Hi,

yes, it really helped me. Thanks alot again!

Dec 22, 2008 at 10:38 PM
Mariano,
Where is this _viewModuleB coming from? Is this the main view from ModuleB? What if there's a main view for a module A and there user is working on this module but on a different view? Then, without finishing the work, user decides to load module B. When done with B,user wants to go back to A and continue working from where he/she left? How would I do this?

Thanks

Dec 23, 2008 at 12:03 PM

Hi Gustavo,

 

Where is this _viewModuleB coming from? Is this the main view from ModuleB?

Yes. The _viewModuleB member holds a reference to a view in ModuleB that was previously created (for example in the Initialize method).

 

What if there's a main view for a module A and there user is working on this module but on a different view? Then, without finishing the work, user decides to load module B. When done with B, user wants to go back to A and continue working from where he/she left? How would I do this?

You can use named views using the overload of the Add method of the region. Then you can get / show the views in ModuleA or ModuleB by using the GetView method. Check the following sample code:

// In the ModuleA

private void OnChangeToViewA(object obj)

{

IRegion detailsRegion = _regionManager.Regions["DetailsRegion"];

List<object> activeViews = new List<object>(detailsRegion.ActiveViews);

object viewModuleA = detailsRegion.GetView("MyViewModuleA");

 

// Remove other views showed in the region.

foreach (object view in activeViews)

{

detailsRegion.Remove(view);

 

// If your region is a ContentControl you can use the Deactivate method instead of removing the view.

// detailsRegion.Deactivate(view);

}

 

if (viewModuleA == null)

{

IViewModuleAPresenter presenter = _container.Resolve<IViewModuleAPresenter>();

view = presenter.View;

detailsRegion.Add(view, "MyViewModuleA");

}

 

mainRegion.Activate(view);

}

 

// In the ModuleB

private void OnChangeToViewB(object obj)

{

IRegion detailsRegion = _regionManager.Regions["DetailsRegion"];

List<object> activeViews = new List<object>(detailsRegion.ActiveViews);

object viewModuleB = detailsRegion.GetView("MyViewModuleB");

 

// Remove other views showed in the region.

foreach (object view in activeViews)

{

detailsRegion.Remove(view);

 

// If your region is a ContentControl you can use the Deactivate method instead of removing the view.

// detailsRegion.Deactivate(view);

}

 

if (viewModuleB == null)

{

IViewModuleBPresenter presenter = _container.Resolve<IViewModuleBPresenter>();

view = presenter.View;

mainRegion.Add(view, "MyViewModuleB");

}

 

mainRegion.Activate(view);

}

You can find more samples in the UI composition Quickstart, where named views are used to show the information of every employee.

 

Please let me know if this helps.

 

Mariano Converti

http://blogs.southworks.net/mconverti