How prevent RegisterViewWithRegion adds new TabItems in PRISM?

Topics: Prism v4 - WPF 4
Jul 24, 2012 at 9:36 AM


In my PRISM app I have 3 modules that each one have 3~6 views. in each module when Initializemethod runs, after each RegisterViewWithRegion for each one of views, new TabItem appears inShell's TabControl. But i want to just add one TabItem at startup and each time user execute aCommand and Navigation occurs, new TabItem appears. Also user should be able to add or remove TabItems. What should i do? Implement a new RegionAdapter or what?

Here is my Shell TabControl:

<TabControl TabStripPlacement="Left" Grid.Column="2" Margin="6" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"
:RegionManager.RegionName="{x:Static infrastructure:RegionNames.MainRagionName}">
<TabItem />

and This is RegisterViewWithRegion:

var codingMainTreeView = _container.Resolve<Views.CodingMainTreeView>();
.RegisterViewWithRegion(RegionNames.MainRagionName, () => codingMainTreeView);

var vouchersMainView = _container.Resolve<Views.VouchersMainView>();
.RegisterViewWithRegion(RegionNames.MainRagionName, () => vouchersMainView
Jul 24, 2012 at 7:49 PM


Based on my understanding, when registering a view in a region, Prism will inject the view a soon as the region becomes available. Therefore, when using this approach you will not have control of when the views will be added to the region.

If you want to control when a view is added or removed from a region (for example, when a command is executed, etc) you could try other UI composition approaches provided by Prism, like view injection or navigation. You can find information about the differences of view discovery and view injection in the following section of the Prism documentation:

For example, if you want to use navigation, you need to register the views in the container (instead of the region) and then perform a navigation request when you navigate to the view. For example, you can register the views like this in the Initialize method (assuming you are using Unity as the container):

//We register the view types in the container for navigation.

//You can still register one of those views in the region, so that it's added on when the region is available.
var codingMainTreeView = _container.Resolve<Views.CodingMainTreeView>();
_regionManager.RegisterViewWithRegion(RegionNames.MainRagionName, () => codingMainTreeView);

Then, when you want to navigate to a view (for example to a VouchersMainView) when a command is executed, you can perform a navigation request:

_regionManager.Regions[RegionNames.MainRagionName].RequestNavigate(new Uri("VouchersMainView",UriKind.Relative));

You can find more information regarding UI composition and navigation in the following chapters of the Prism documentation:

I hope you find this useful,

Damian Cherubini