【问题标题】:Overlapping Transparent Views in a Prism Region在棱镜区域中重叠透明视图
【发布时间】:2020-09-07 05:33:15
【问题描述】:

我想知道是否有一种方法可以在一个命名区域中显示两个重叠的透明视图?下面的示例显示了两个重叠的透明视图,它们位于第一个网格行中各自独立的命名区域容器中。在第二个网格行中,我们有一个名为“RegionC”的区域。第一个注册的视图是显示的视图(“ViewA”)。我是否正确,如果我们有多个视图注册到一个区域,那么我们一次只能显示一个视图?有没有办法在一个命名区域中显示两个重叠的视图?或者添加另一个内容控件以支持显示的多个视图是标准做法吗?我想要这样做的一个原因是,我可以更好地将我的 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 而不是ContentControl 代表RegionC
  • 我试过了,但它只是垂直堆叠视图,实际上并没有重叠。
  • ItemsControl 上将ItemsPanel 属性设置为Grid
  • 是的,就是这样!谢谢。

标签: c# wpf prism


【解决方案1】:

您应该为您的区域使用ItemsControl 而不是ContentConrol RegionC 并将其ItemsPanel 设置为Grid

这是 XAML:

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

【讨论】:

    【解决方案2】:

    如果我们有多个视图注册到一个区域,那么我们一次只能显示一个视图,我是否正确?

    对于ContentControl 中的区域,是的,您是正确的,因为它一次只能有一个Content。但基本上任何控件都可以托管一个区域(前提是您创建了关联的regionAdapter),例如,ItemsControlTabControl 可以同时在其中拥有多个视图。

    有没有办法在一个命名区域中显示两个重叠的视图?

    是的,例如,如果您为Grid 提供RegionAdapter

    或者添加另一个内容控件以支持显示的多个视图是标准做法吗?

    是和不是。多个区域的多个视图的标准,而不是同一区域的多个视图的标准。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多