Moisej Braver
Moisej Braver

Reputation: 317

Prism WPF Dynamic Regions

Let's say we have a Prism 7 application with modules A and B. The main window has a Tab Control and two buttons, which add either module A or B to the Tab Control. I created a binding for the Tab Control items and implemented an item template, which includes Prism Region, whose name is bound to the item name.

<TabControl ItemsSource="{Binding Tabs}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </TabControl.ItemTemplate>
            
        <TabControl.ContentTemplate>
            <DataTemplate>
                <ContentControl prism:RegionManager.RegionName="{Binding}" />
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>

The problem I am facing is that the region name doesn't seem to change, i.e. if I first add Module A, all the next button clicks will add Module A and vice versa. In my previous question Prism WPF Binding RegionManager.RegionName I was explained that I shouldn't bind the RegionManger.RegionName, so my question is, how should I implement the requirements?

Link to the repo: https://github.com/moisejbraver/ModulesTest

Upvotes: 3

Views: 1153

Answers (1)

Athul Raj
Athul Raj

Reputation: 189

This is the way i handle tab controls with prism regions, i think this is helpful for you too.

<TabControl prism:RegionManager.RegionName="{x:Static local:RegionNames.AdvancedSetup}">
    <TabControl.ItemContainerStyle>
        <Style TargetType="TabItem">
            <Setter Property="Visibility" Value="{Binding DataContext.IsAvailable, Converter={coverters:BooleanToVisibilityConverter}}"/>
            <Setter Property="Header" Value="{Binding DataContext.Name}"/>
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

Then i register all my tabs to the same region in OnInitialized function of each module.

regionManager.RegisterViewWithRegion(RegionNames.AdvancedSetup, setupType);

I've made an interface which all viewmodels of tabs will implement, it contains

  1. Name of the tab
  2. Whether the tab is visible.

You may need to change some details based on your need.

Upvotes: 3

Related Questions