您需要做的是ContentControl
在主视图中使用 a 来显示ConnectedSystem
主视图模型的属性。通过使用 aContentControl
您将被包含在视图模型绑定过程中,并且将应用视图模型绑定器规则。因此,您希望您的属性(使用 Caliburn 的默认实现)具有类型ConnectedSystemViewModel
并具有名为ConnectedSystemView
. 然后在用于显示您想要的父视图的视图ContentControl
中x:Name
(ConnectedSystem
ConnectedSystemViewModel 属性的名称。这将导致视图模型绑定器连接两者并执行其通常的工作。为了清楚起见,这里有一些代码:
ConnectedSystemView.xaml(约定在指定ContentControl
为显示主视图模型的已连接系统属性的控件时将使用的用户控件)
<UserControl x:Class="Sample.Views.ConnectedSystemView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Column="0"
Grid.Row="0">PropertyOne Label:</Label>
<TextBox x:Name="PropertyOne"
Grid.Column="1"
Grid.Row="0"></TextBox>
<TextBlock Grid.Column="0"
Grid.Row="1">PropertyTwo Label:</TextBlock>
<TextBox x:Name="PropertyTwo"
Grid.Column="1"
Grid.Row="1"></TextBox>
<!-- repeat the TextBlock, TextBox pair for the remaining
properties three through ten -->
</Grid>
</UserControl>
ConnectedSystemViewModel.cs(主视图模型上的 ConnectedSystem 属性的类型)
namespace Sample.ViewModels
{
public class ConnectedSystemViewModel : PropertyChangedBase
{
private string _propertyOne;
public string PropertyOne
{
get { return _propertyOne; }
set
{
_propertyOne = value;
NotifyOfPropertyChange(() => PropertyOne);
}
}
// these all need to be as above with NotifyPropertyChange,
// omitted for brevity.
public string PropertyTwo { get; set;}
public string PropertyThree { get; set;}
public string PropertyFour { get; set;}
public string PropertyFive { get; set;}
public string PropertySix { get; set;}
public string PropertySeven { get; set;}
public string PropertyEight { get; set;}
public string PropertyNine { get; set;}
public string PropertyTen { get; set;}
}
}
并在您的主视图中定义一个相对于类型的主视图模型属性命名的 ContentControlConnectedSystemViewModel
<ContentControl x:Name="ConnectedSystem"></ContentControl>
如果我正确理解你的问题,这应该是你需要加入默认的 Caliburn.Micro 约定的全部内容。显然,您将添加 10 个ConnectedSystem
属性ConnectedSystemViewModel
和具有适当名称的适当控件ConnectedSystemView
以完成实现。
这样,在您的主视图中,您只需要定义一个ContentControl
来显示 ConnectedSytem 属性(而不是 10 个相同的自定义用户控件),并且约定将确定用于填充ContentControl
.
在ConnectedSystemView
其中将ContentControl
通过约定插入到主视图的内容属性中,您拥有要显示 10 个连接的系统属性的控件。