If you have a UserControl that can be used in more than once in the same view, and it has a region, you'll have to show the user control in a scoped region. The CompositeWPF documentation provides details on this feature at the following location:
Composite Application Guidance for WPF - June 2008
+ Development Activities
++ How to: Show a View in a Scoped Region
Reference CodePlex message
For this demo we want to place an instance of UserControl2 within the UCRegion of UserControl1 - the catch is we want to place two instances of UserControl1 on the same view
// Our view's host has two regions labled lblRegion and lblRegion2
// both will hold the same UserControl1
IRegion lblRegion = RegionManager.Regions["lblRegion"];
IRegion lblRegion2 = RegionManager.Regions["lblRegion2"];
// We'll create two instances of the same control
UserControl1 uc1 = new UserControl1();
UserControl1 uc2 = new UserControl1();
// Let's add our two controls to the applicable regions. We'll get a reference
// to the returned scoped region manager and use it to add an instance of UserControl2.
// Note that we set the third parameter (createRegionManagerScope) to true
IRegionManager uc1RegMgr = lblRegion.Add(uc1, "UserControl1", true);
// Use the existing region manager to add uc2
lblRegion2.Add(uc2, "UserControl2");
// Add UserControl2 to both UserControl1's UCRegion
uc1RegMgr.Regions["UCRegion"].Add(new UserControl2());
RegionManager.Regions["UCRegion"].Add(new UserControl2());
Note that we didn't Activate the region as suggested by step 4 of the above referenced documentation. The reason is that we modified the Composite.Wpf \ Regions \ Region.cs file as follows:
public virtual IRegionManager Add(object view, string viewName, bool createRegionManagerScope)
{
IRegionManager regionManager = createRegionManagerScope
? this.RegionManager.CreateRegionManager() : this.RegionManager;
InnerAdd(view, viewName, regionManager);
// BillKrat.2008.08.18 - activate view after adding it so
// that the region.ActiveViews.CollectionChanged event will
// fire in ContentControlRegionAdapter (updates content)
if (this is SingleActiveRegion)
Activate(view);
return regionManager;
} // Reference CodePlex Issue
This works because our UserControl will use the ContentControlRegionAdapter which returns a type of SingleActiveRegion.
Tags:
compositewpf,
regionmanager
Categories:
CompositeWPF