我想知道是否有办法在一个命名的Region中显示两个重叠的透明视图?下面的示例在第一行中的各自独立的命名区域容器中显示了两个重叠的透明视图。在第二个网格行中,我们有一个名为“ RegionC”的区域。第一个注册的视图是显示的视图(“ ViewA”)。我是否正确,如果我们在一个区域注册了多个视图,那么一次只能显示一个视图?有没有一种方法可以在一个命名的Region中显示两个重叠的视图?还是添加另一个内容控件以支持显示的多个视图是标准做法?我想要这样做的原因之一是,我可以更好地在单独的视图中分离XAML代码,然后根据需要将它们注入一个区域容器中。
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" />
您应该在区域ItemsControl
中使用ContentConrol
而不是RegionC
,并将其ItemsPanel
设置为Grid
。
这里是XAML:
<ItemsControl Grid.Row="1" prism:RegionManager.RegionName="RegionC">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
我是否纠正,如果我们在一个区域中注册了多个视图,那么我们一次只能显示一个视图?
对于ContentControl
中的区域,是的,您是正确的,因为一次只能有一个Content
。但是基本上任何控件都可以托管一个区域(前提是您创建了关联的regionAdapter
),例如ItemsControl
或TabControl
可以同时在其中包含多个视图。
是否有办法在一个命名的Region中显示两个重叠的视图?
是,例如,如果为RegionAdapter
提供Grid
。
或者是添加另一个内容控件来支持显示的多个视图的标准做法?
是,不是。对于多个区域中的多个视图是标准的,而不是同一区域中的多个视图的标准。