Mutiple views in same region

Nov 24, 2008 at 3:36 PM
In my sample app I am working on, I need to show detail data of multiple customers at the same time. I have  one CustomerDetail user control (view) and trying to add multiple instances of that view into same region fails because (as it looks to me now) CAG prevents that.

Any explanation, why is that forbiden and (if possible) how to override that constrain? Should I define ItemsControl based local region and start loading my views there?

Thanks!
Nikola
Nov 25, 2008 at 6:05 PM

Hi Nikola,

 

There should not be any problem when adding different instances of the same view to a region:

MyView viewInstance1 = new MyView();

MyView viewInstance2 = new MyView ();

IRegion mainRegion = this.regionManager.Regions["MainRegion"];

 

mainRegion.Add(viewInstance1);

mainRegion.Add(viewInstance2);

 

What you should not do is add the same instance of the view multiple times to the same region:

MyView view = new MyView();

IRegion mainRegion = this.regionManager.Regions["MainRegion"];

 

mainRegion.Add(view);

mainRegion.Add(view); //This will throw an exception: "View already exists in region"

 

Keep in mind that you should use the Activate method to display a view that has already been added to a ContentControl region:

MyView view = new MyView();

IRegion mainRegion = this.regionManager.Regions["MainRegion"];

 

mainRegion.Add(view);

//...

mainRegion.Activate(view);

 

Perhaps you will be interested on using scoped regions. A region scope is delimited by a parent view and embraces all the child views of the parent view.

·         How to: Show a View in a Scoped Region

·         The UI Composition QuickStart uses a region scope associated to the EmployeesDetailsView. All the regions registered by the this view aren’t registered with the Shell window's region manager; instead, they are registered with a new region manager associated to it.

 

Please let me know if this helps.

 

Mariano Converti

http://blogs.southworks.net/mconverti