Inserting a View in more than one Region

Topics: Prism v2 - WPF 3.5
Jan 5, 2010 at 2:09 PM

Hi,

I need to insert a view in more than one region. When the user manipulates this view
will it, and it's associated ViewModel, be changed equally in both regions ???

That is when I insert a view in a Region am I inserting a brand new copy or simply a
reference of the View.

I need to do it because my user will have the option to swap regions (a content
region and a Tab region) and, of course, he would like that the modifications done
in one view be maintained when he swaps the regions.

Thanks in advance

Jan 6, 2010 at 4:32 PM
Edited Jan 6, 2010 at 4:33 PM

Hi,

Let’s suppose you are constructing your views in the following way:

IMyView view = container.Resolve<IMyView>();

Unity’s default behavior, unless you have registered any specific instance of the view in the container or marked the view to be a singleton, will be returning a brand new instance of the view. Thus, the code below will provide you with two different instances:

IMyView view1 = container.Resolve<IMyView>();
IMyView view2 = container.Resolve<IMyView>();
In the scenario when you do want to obtain the same view instance, you could keep a reference to that view and add it to the various regions or mark it as a singleton in the Unity container.

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

 

Jan 6, 2010 at 5:45 PM

Thanks Damian,

So I could do:

IMyView view = container.Resolve<IMyView>();

region1.Add(view);
region2.Add(view);

region1 is hidden and only region2 is displayed.

This way I beleive that when the view in the displayed region changes because of
databindings or any update controlled by the ViewModel it will work ok. That is the hidden
view will be updated the same way.

But if the view shows a messagebox or something like this. Will the user input be
reflected in the hidden view ???

Thanks in advance

 


 

Jan 7, 2010 at 5:58 PM

Hi,

The approach you are suggesting above is not a viable one, as you would be trying to make the view a logical child of more than one control at the same time. The exception you will get if you have two content controls for instance is an InvalidOperationException with the following message "Specified element is already the logical child of another element. Disconnect it first." (other controls might not throw this same exception but it will not work anyways).

You can take advantage of the fact that one of the regions is hidden, to remove the view from one of the regions and place it in the other one.

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman