我对 Caliburn.Micro 很陌生,所以我想这有一个简单的答案(或者至少我希望它有 :))
我有一个 ViewModel,它有一个名为 ConnectedSystem
的属性,它有一个名为 Name
的子属性。
在我看来,我有以下 XAML(摘录):
<StackPanel Orientation="Horizontal">
<Label Content="Name:" />
<TextBlock x:Name="ConnectedSystem_Name" />
</StackPanel>
这工作得很好,名称按预期显示在 TextBlock
中。但是,ConnectedSystem 有大约 10 个应该显示的属性,我不想在我的 View 中复制粘贴 10 次以上的 XAML。相反,我想将该 XAML 提取为 UserControl,我可以在其中将 LabelText 和 Text 设置为属性。
我试过了,但我不确定如何让 Caliburn.Micro 自动将 ConnectedSystem_Name 传递到我的 UserControl。
这可能也是一种比在这里使用 UserControl 更好的方法,所以问题基本上是:将此共享 XAML 作为它自己的控件的最佳方法是什么,并且仍然能够使用 Caliburn.Micros 绑定(bind)。
最佳答案
您需要做的是在主视图中使用 ContentControl
来显示主视图模型的 ConnectedSystem
属性。通过使用 ContentControl
,您将被包含在 View 模型绑定(bind)过程中,并且将应用 View 模型绑定(bind)器规则。因此,您希望您的属性(使用 Caliburn 的默认实现)属于 ConnectedSystemViewModel
类型,并有一个名为 ConnectedSystemView
的 View 。然后在用于显示父级的 View 中,您需要一个 ContentControl
,其 x:Name
为 ConnectedSystem
(ConnectedSystemViewModel 属性的名称。这将使 View 模型联编程序连接两者并执行其通常的工作。为了清楚起见,这里有一些代码:
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;}
}
}
并在您的主视图中定义一个 ContentControl,其名称相对于 ConnectedSystemViewModel
类型的主视图模型属性命名
<ContentControl x:Name="ConnectedSystem"></ContentControl>
如果我理解你的问题是正确的,这应该是你需要 Hook 到默认的 Caliburn.Micro 约定中的全部内容。显然,您会将 10 个 ConnectedSystem
属性添加到 ConnectedSystemViewModel
并将具有适当名称的适当控件添加到 ConnectedSystemView
以完成实现。
这样在您的主视图中您只需要定义一个 ContentControl
来显示 ConnectedSytem 属性(而不是 10 个相同的自定义用户控件)并且约定将确定要使用的用户控件类型填充 ContentControl
。
在 ConnectedSystemView
中,它将通过约定插入到您的主视图 ContentControl
的内容属性中,您拥有要显示 10 个已连接系统属性的控件。
关于c# - 在 "inner"用户控件上使用 Caliburn.Micro 绑定(bind)功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6014196/