Create a Custom Prism RegionAdapter
Don’t want to read the article? Watch the video tutorial on Xaml TV.
Prism provides 4 region adapters out of the box for you:
- TabControlRegionAdapter (Silverlight only)
Well, what happens when you want to use a different control as a region host? Simple. You need to write a custom region adapter for it. Is it hard you ask? No it is quite easy. Let’s write one for the StackPanel.
Start by creating a class the derive from and implements the base abstract class RegionAdapterBase<T>.
public StackPanelRegionAdapter(IRegionBehaviorFactory factory)
protected override void Adapt(IRegion region, StackPanel regionTarget)
region.Views.CollectionChanged += (s, e) =>
if (e.Action == NotifyCollectionChangedAction.Add)
foreach (FrameworkElement element in e.NewItems)
protected override IRegion CreateRegion()
return new AllActiveRegion();
Notice that there are two methods we need to implement. Adapt and CreateRegion. CreateRegion return the type of region we will need. In our case we want to support multiple views so we need to return an instance of an AllActiveRegion. If we only needed support for one view at a time we would return a SingleActiveRegion. The Adapt method is responsible for adapting the region to our control. This is where we will add and remove the views to or host control.
Now we simply have to tell Prism about our new RegionAdapter. We do this in the bootstrapper. Simply override the ConfigureRegionAdapterMappings method as follows:
RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
That’s it. Now you can use a StackPanel as a region host:
One thought on “Create a Custom Prism RegionAdapter”
Good stuff, Brian.
Comments are closed.