【问题标题】:Two dynamically assigned ContentControls in single view in Caliburn.MicroCaliburn.Micro 中单个视图中的两个动态分配的 ContentControl
【发布时间】:2011-10-17 02:38:41
【问题描述】:
我有一个 UserControl,其中包含两个 ContentControl,它们需要在运行时绑定不同的 UserControl 视图。注明here 的附加属性解决方案似乎在 Silverlight 中不起作用。或者,我做错了什么。我也找到了this,但也没有带来什么快乐。
我有一个 ContentControl 工作,将其命名为“ActiveItem”。但是,当然,我不能有两个同名的 ContentControl。
提前感谢您的帮助,
吉姆
【问题讨论】:
标签:
silverlight
silverlight-4.0
mvvm
caliburn.micro
contentcontrol
【解决方案1】:
这是一个老问题,但如果有人遇到同样的问题,我在这里发布我从一开始就以更彻底的方式处理它的方式:
- 包含两个(甚至两个以上)用户控件的主窗口必须继承自
Caliburn.Micro.Conductor<Screen>.Collection.AllActive;
- 您的用户控件必须继承自
Caliburn.Micro.Screen;
- 您还必须牢记命名约定。如果您在 View 中使用 MenuUC 作为 ContentControl 的名称,还要在 ViewModel 中创建一个名为 MenuUC 的属性;
- 像我在 Constructor 中一样初始化您的 UserControl;
- 现在您可以在代码中的任何位置使用
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 question,它可能会对您有所帮助。
【解决方案2】:
只需在您的主视图模型上公开两个公共属性,每个都是您希望显示的视图模型类型的一个实例。然后,在您的视图中有一个具有相应名称的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" />