namg_engr
namg_engr

Reputation: 359

Overlapping Transparent Views in a Prism Region

I was wondering if there is a way to show two overlapping transparent views in one named Region? My example below shows two overlapped transparent views in their own separate named region container in the first grid row. In the second grid row we have one named region "RegionC". The first registered view is the one that is displayed ("ViewA"). Am I correct that if we have multiple views registered to a region then we can only show one view at a time? Is there a way to show two overlapped views in one named Region? Or is it standard practice to add another content control to support multiple views shown? One reason why I want to do this is so I can better separate my XAML code in separate views and inject them into one region container as needed.

ShellWindow.xaml

<Window x:Class="PrismDemo.Views.ShellWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:prism="http://prismlibrary.com/"
    prism:ViewModelLocator.AutoWireViewModel="True"
    Title="{Binding Title}" Height="150" Width="325" >
<Grid ShowGridLines="True">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <ContentControl Grid.Row="0"  prism:RegionManager.RegionName="RegionA" />
    <ContentControl Grid.Row="0"  prism:RegionManager.RegionName="RegionB" />
    <ContentControl Grid.Row="1"  prism:RegionManager.RegionName="RegionC" />
</Grid>

ShellWindow.xaml.cs

using Prism.Regions;
using System.Windows;

namespace PrismDemo.Views
{
    public partial class ShellWindow : Window
    {
        public ShellWindow(IRegionManager regionManager)
        {
            InitializeComponent();

            regionManager.RegisterViewWithRegion("RegionA", typeof(ViewA));
            regionManager.RegisterViewWithRegion("RegionB", typeof(ViewB));
            regionManager.RegisterViewWithRegion("RegionC", typeof(ViewA));
            regionManager.RegisterViewWithRegion("RegionC", typeof(ViewB));
        }
    }
}


ViewA.xaml

<UserControl x:Class="PrismDemo.Views.ViewA"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:PrismDemo.Views"
         mc:Ignorable="d" 
         d:DesignHeight="200" d:DesignWidth="300">
<TextBlock Text="ViewA" FontSize="20" />

ViewB.xaml

<UserControl x:Class="PrismDemo.Views.ViewB"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:PrismDemo.Views"
         mc:Ignorable="d" 
         d:DesignHeight="200" d:DesignWidth="300">
<TextBlock Text="ViewB" FontSize="30" />

Upvotes: 0

Views: 182

Answers (2)

Suresh
Suresh

Reputation: 4149

You should use ItemsControl instead of ContentConrol for your region RegionC and set its ItemsPanel to Grid.

Here is the XAML:

    <ItemsControl Grid.Row="1"  prism:RegionManager.RegionName="RegionC">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid IsItemsHost="True" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

Upvotes: 2

Haukinger
Haukinger

Reputation: 10863

Am I correct that if we have multiple views registered to a region then we can only show one view at a time?

For regions in a ContentControl, yes, you are correct, because it can only have one Content at a time. But basically any control can host a region (provided you create an associated regionAdapter), for example, an ItemsControl or a TabControl can have multiple views inside them at the same time.

Is there a way to show two overlapped views in one named Region?

Yes, if you provide a RegionAdapter for a Grid, for example.

Or is it standard practice to add another content control to support multiple views shown?

Yes and no. Standard for multiple views in multiple regions, not standard for multiple views in the same region.

Upvotes: 1

Related Questions