为什么 ItemsControl 显示视图但 ContentControl 什么也不显示?

发布于 2024-07-27 20:34:19 字数 1329 浏览 5 评论 0原文

我有一个基于 Prism 的应用程序。

这是我的 shell:

<Window x:Class="AvarioCRM3.ShellV2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cal="http://www.codeplex.com/CompositeWPF" >

    <DockPanel LastChildFill="True">
        <Border
            Padding="10"
            DockPanel.Dock="Top"
            Background="#ddd">
            <DockPanel>
                <ItemsControl 
                    Name="MainNavigationPanel" 
                    cal:RegionManager.RegionName="MainNavigationPanel" 
                    DockPanel.Dock="Top"/>

            </DockPanel>
        </Border>
    </DockPanel>

</Window>

在我的 MenuModule 中,我向该区域添加了一个视图,它显示得很好:

public void Initialize()
{
    MainNavigationPresenter mainNavigationPresenter = this.container.Resolve<MainNavigationPresenter>();
    IRegion mainRegion = this.regionManager.Regions["MainNavigationPanel"];
    mainRegion.Add(new TestView());
}

问题是:我不想在我的 shell 中使用 ItemsControl,我想要一个 ContentControl,但是当我使用 ContentControl 时,它什么也不显示。

为什么 ItemsControl 显示我的视图而 ContentControl 不显示任何内容?

I've got an application based on Prism.

This is my shell:

<Window x:Class="AvarioCRM3.ShellV2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cal="http://www.codeplex.com/CompositeWPF" >

    <DockPanel LastChildFill="True">
        <Border
            Padding="10"
            DockPanel.Dock="Top"
            Background="#ddd">
            <DockPanel>
                <ItemsControl 
                    Name="MainNavigationPanel" 
                    cal:RegionManager.RegionName="MainNavigationPanel" 
                    DockPanel.Dock="Top"/>

            </DockPanel>
        </Border>
    </DockPanel>

</Window>

In my MenuModule I add a view to the region and it shows fine:

public void Initialize()
{
    MainNavigationPresenter mainNavigationPresenter = this.container.Resolve<MainNavigationPresenter>();
    IRegion mainRegion = this.regionManager.Regions["MainNavigationPanel"];
    mainRegion.Add(new TestView());
}

The problem is: I don't want an ItemsControl in my shell, I want a ContentControl, but when I use a ContentControl, it shows nothing.

Why would ItemsControl show my views and ContentControl show nothing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

究竟谁懂我的在乎 2024-08-03 20:34:19

这可能是因为 ContentControl 仅显示一个子项,而 ItemsControl 有多个子项吗?

我没有使用过 Prism,但 API 表明 IRegion 应该有多个子区域。 如果您使用的是 ContentControl,那么当我执行以下操作时会发生什么有点不明确:

IRegion mainRegion = this.regionManager.Regions["MainNavigationPanel"];
mainRegion.Add(new TestView());
mainRegion.Add(new SecondTestView());

Could this be because a ContentControl will only display a single child, whereas an ItemsControl has multiple children?

I have't worked with Prism, but the API suggests that an IRegion is expected to have multiple children. If you're using a ContentControl then it is a little ambiguous what happens when I do the following:

IRegion mainRegion = this.regionManager.Regions["MainNavigationPanel"];
mainRegion.Add(new TestView());
mainRegion.Add(new SecondTestView());
最后的乘客 2024-08-03 20:34:19

与具有 ContentControl 的 ItemsControl 不同,您还需要在添加视图后激活视图以使其可见。

MainNavigationPresenter mainNavigationPresenter = this.container.Resolve<MainNavigationPresenter>();
IRegion mainRegion = this.regionManager.Regions["MainNavigationPanel"];
TestView view = new TestView()
mainRegion.Add(view);
mainRegion.Activate(view);

Unlike the ItemsControl with a ContentControl you also need to activate the view once you have added it to make it visible.

MainNavigationPresenter mainNavigationPresenter = this.container.Resolve<MainNavigationPresenter>();
IRegion mainRegion = this.regionManager.Regions["MainNavigationPanel"];
TestView view = new TestView()
mainRegion.Add(view);
mainRegion.Activate(view);
谜兔 2024-08-03 20:34:19

我注意到您在初始化中执行此操作。 会不会太早了? 您是否尝试过使用注册而不是注入视图来查看是否改变了什么?

regionManager.RegisterViewWithRegion("MainNavigationPanel", typeof(TestView));

这不会解决您的问题,但它将证明问题是在您的区域实际可用之前尝试添加某些内容。 RegisterViewWithRegion 将延迟视图的创建和显示,直到该区域可用。

I noticed you are doing this in Initialize. Could be too early? Have you tried using registration rather than injection of your view to see if that changed anything?

regionManager.RegisterViewWithRegion("MainNavigationPanel", typeof(TestView));

This won't solve your problem, however it will prove that the problem is trying to add something before your region is actually available. RegisterViewWithRegion will delay the creation and display of the view until the region is available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文