Caliburn.Micro-如何从继承的ViewModel在WPF视图中显示多个项目:Conductor 。Collection.AllActive

问题描述 投票:0回答:1

我正在使用Caliburn.Micro作为WPF应用程序中的MVVM框架。我试图在一个页面中包含多个自定义用户控件,这些控件会基于用户交互而动态更改。我了解CM约定是,视图将绑定到ViewModel中的Items属性。

我的问题是:

  1. 我如何在视图中访问ViewModel的Items集合属性内的每个项目?

  2. 我是否可以像通常使用普通的WPF控件那样将每个项目分别放置在父视图页面上?] >>

    此处显示了视图页面的基本外观。View page layout

  3. 基本的ViewModel代码:

  public class TestViewModel : Conductor<object>.Collection.AllActive
    {
        public TestViewModel()
        {
            Items.Add(new CustomUC1ViewModel());
            Items.Add(new CustomUC2ViewModel());
            Items.Add(new CustomUC3ViewModel());
        }
        //following logic would reassign the Items collection as required
    }

附加到Items的ViewModels将基于与父页面和嵌入式用户控件的用户交互而动态更改。

我希望以上内容有意义(这是我的第一篇文章)。>

我正在使用Caliburn.Micro作为WPF应用程序中的MVVM框架。我试图在一个页面中包含多个自定义用户控件,这些控件会基于用户交互而动态更改。我了解...

我已经解决了我的问题。通过在ViewModel中将用户控件视图分配为IScreen属性,我可以通过命名ContentControl来匹配ViewModel中的相关属性,从而在视图中所需的位置找到用户控件。如果使用设置器中的NotifyOfPropertyChange(),则可以更改ViewModel中的相关属性,并相应地更新视图。

在MyViewModel中:

public class MyViewModel : Conductor<object>.Collection.AllActive 
{
    //the user control property where MainContent = new OtherViewModel()
    private IScreen _mainContent;
    public IScreen MainContent
    {
        get { return _mainContent; }
        set 
        {
            _mainContent = value;
            NotifyOfPropertyChange(() => MainContent);
        }
    }
}

在XAML中的MyView中:

<ContentControl x:Name="MainContent"/>

ViewModel中的[Items]属性仅用于管理活动窗口的生命周期。我能够使用多个<ContentControl x:name="ViewModelProperty">根据需要并动态地放置自定义用户控件。

c# wpf xaml mvvm caliburn.micro
1个回答
0
投票

我已经解决了我的问题。通过在ViewModel中将用户控件视图分配为IScreen属性,我可以通过命名ContentControl来匹配ViewModel中的相关属性,从而在视图中所需的位置找到用户控件。如果使用设置器中的NotifyOfPropertyChange(),则可以更改ViewModel中的相关属性,并相应地更新视图。

© www.soinside.com 2019 - 2024. All rights reserved.