Caliburn.Micro 中单个视图中的两个动态分配的 ContentControl

发布于 2024-11-27 00:31:55 字数 423 浏览 1 评论 0原文

我有一个 UserControl,其中包含两个 ContentControl,这两个 ContentControl 需要在运行时将不同的 UserControl 视图绑定到它们。此处提到的附加属性解决方案确实如此似乎在 Silverlight 中不起作用。或者,我做错了什么。我还发现这个< /a>,但也没有带来任何快乐。

我有一个 ContentControl,将其命名为“ActiveItem”。但是,当然,我不能有两个同名的 ContentControl。

预先感谢您的帮助,

吉姆

I have a UserControl that contains two ContentControls that need to have different UserControl Views bound to them at runtime. The attached-Property solution noted here does not seem to work in Silverlight. Or, I am doing something wrong. I also, found this, but it did not bring any joy either.

I had a single ContentControl working by naming it 'ActiveItem'. But, of course, I cannot have two ContentControls with the same name.

Thanks in advance for any help,

Jim

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

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

发布评论

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

评论(2

鹤舞 2024-12-04 00:31:55

只需在主视图模型上公开两个公共属性,每个属性都是您希望显示的视图模型类型的实例。然后,在您的视图中有一个具有相应名称的 ContentControl。例如:

public class MyMainViewModel
{
  private NavigationViewModel navigation;
  private MyContentViewModel main;

  public MyMainViewModel()
  {
    // better to inject factories using constructor injection here
    this.Navigation = new NavigationViewModel();
    this.Main = new MyContentViewModel();
  }

  public NavigationViewModel Navigation
  {
    get { return navigation; }
    set { navigation= value; NotifyOfPropertyChanged(() => this.Navigation); }
  }

  public MyContentViewModel Main
  {
    get { return main; }
    set { main= value; NotifyOfPropertyChanged(() => this.Main); }
  }

  ...
}

<ContentControl x:Name="Navigation" />
...
<ContentControl x:Name="Main" />

Just expose two public properties on your main view model, each one being an instance of the type of view model you wish to display. Then, in your view have a ContentControl with the corresponding name. E.g:

public class MyMainViewModel
{
  private NavigationViewModel navigation;
  private MyContentViewModel main;

  public MyMainViewModel()
  {
    // better to inject factories using constructor injection here
    this.Navigation = new NavigationViewModel();
    this.Main = new MyContentViewModel();
  }

  public NavigationViewModel Navigation
  {
    get { return navigation; }
    set { navigation= value; NotifyOfPropertyChanged(() => this.Navigation); }
  }

  public MyContentViewModel Main
  {
    get { return main; }
    set { main= value; NotifyOfPropertyChanged(() => this.Main); }
  }

  ...
}

<ContentControl x:Name="Navigation" />
...
<ContentControl x:Name="Main" />
星星的軌跡 2024-12-04 00:31:55

这是一个老问题,但如果有人遇到同样的问题,我在这里发布我从一开始就以更彻底的方式处理它的方法:

  1. 您的主窗口包含两个(甚至两个以上)用户控件必须继承自Caliburn.Micro.Conductor.Collection.AllActive
  2. 您的用户控件必须继承自Caliburn.Micro.Screen
  3. 您还必须牢记命名约定。如果您在 View 中使用 MenuUC 作为 ContentControl 的名称,则还要在 ViewModel 中创建一个名为 MenuUC 的属性;
  4. 像我在构造函数中一样初始化您的用户控件;
  5. 现在,您可以在代码中的任何位置使用 ActivateItem(MenuUC)DeactivateItem(MenuUC)。 Caliburn.Micro 会自动检测您想要使用哪一个。

示例 XAML 查看代码:

<Window x:Class="YourProject.Views.YourView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="YourViewTitle" Width="900" Height="480">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="4*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Menu Side Bar -->
        <ContentControl Grid.Row="0" Grid.Column="0" x:Name="MenuUC" />

        <!-- Panel -->
        <Border Grid.Column="1" Grid.RowSpan="2" BorderThickness="1,0,0,0" BorderBrush="#FF707070" >
            <ContentControl x:Name="PanelUC" />
        </Border>
    </Grid>
</Window>

C# ViewModel 代码示例:

class YourViewModel : Conductor<Screen>.Collection.AllActive
{
    // Menu Side Bar
    private MenuUCViewModel _menuUC;
    public MenuUCViewModel MenuUC
    {
        get { return _menuUC; }
        set { _menuUC = value; NotifyOfPropertyChange(() => MenuUC); }
    }

    // Panel
    private Screen _panelUC;
    public Screen PanelUC
    {
        get { return _panelUC; }
        set { _panelUC = value; NotifyOfPropertyChange(() => PanelUC); }
    }

    // Constructor
    public YourViewModel()
    {
        MenuUC = new MenuUCViewModel();
        ActivateItem(MenuUC);

        PanelUC = new FirstPanelUCViewModel();
        ActivateItem(PanelUC);
    }

    // Some method that changes PanelUC (previously FirstPanelUCViewModel) to SecondPanelUCViewModel
    public void ChangePanels()
    {
        DeactivateItem(PanelUC);
        PanelUC = new SecondPanelUCViewModel();
        ActivateItem(PanelUC);
    }
}

在上面的示例中,ChangePanels() 充当将新用户控件加载到 ContentControl 中的方法。

另请阅读这个问题,它可能会进一步帮助您。

This is an old question, but in case anyone is having the same issue, I post here my way of handling it from the beginning and in a more thorough manner:

  1. Your main window that contain both (or even more than two) of your User Controls must be inherited from Caliburn.Micro.Conductor<Screen>.Collection.AllActive;
  2. Your User Controls must be inherited from Caliburn.Micro.Screen;
  3. You must also keep naming conventions in mind. If you use MenuUC as the name of a ContentControl in your View, also create a property named MenuUC in your ViewModel;
  4. Initialize your UserControl as I do in Constructor;
  5. Now you can use ActivateItem(MenuUC) and DeactivateItem(MenuUC) everywhere in your code. Caliburn.Micro automatically detects which one you want to work with.

Example XAML View code:

<Window x:Class="YourProject.Views.YourView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="YourViewTitle" Width="900" Height="480">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="4*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Menu Side Bar -->
        <ContentControl Grid.Row="0" Grid.Column="0" x:Name="MenuUC" />

        <!-- Panel -->
        <Border Grid.Column="1" Grid.RowSpan="2" BorderThickness="1,0,0,0" BorderBrush="#FF707070" >
            <ContentControl x:Name="PanelUC" />
        </Border>
    </Grid>
</Window>

Example C# ViewModel code:

class YourViewModel : Conductor<Screen>.Collection.AllActive
{
    // Menu Side Bar
    private MenuUCViewModel _menuUC;
    public MenuUCViewModel MenuUC
    {
        get { return _menuUC; }
        set { _menuUC = value; NotifyOfPropertyChange(() => MenuUC); }
    }

    // Panel
    private Screen _panelUC;
    public Screen PanelUC
    {
        get { return _panelUC; }
        set { _panelUC = value; NotifyOfPropertyChange(() => PanelUC); }
    }

    // Constructor
    public YourViewModel()
    {
        MenuUC = new MenuUCViewModel();
        ActivateItem(MenuUC);

        PanelUC = new FirstPanelUCViewModel();
        ActivateItem(PanelUC);
    }

    // Some method that changes PanelUC (previously FirstPanelUCViewModel) to SecondPanelUCViewModel
    public void ChangePanels()
    {
        DeactivateItem(PanelUC);
        PanelUC = new SecondPanelUCViewModel();
        ActivateItem(PanelUC);
    }
}

In the above example, ChangePanels() acts as a method to load new User Control into your ContentControl.

Also read this question, it might be help you further.

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