在“内部”用户控件上使用Caliburn.Micro绑定功能

|| 我对Caliburn.Micro很陌生,所以我想这有一个简单的答案(或者至少我希望它有:)) 我有一个ViewModel,其属性名为
ConnectedSystem
,其子属性为
Name
。 在我看来,我有以下XAML(摘录):
<StackPanel Orientation=\"Horizontal\">
  <Label Content=\"Name:\" />
  <TextBlock x:Name=\"ConnectedSystem_Name\" />
</StackPanel>
这可以正常工作,并且名称按预期显示在
TextBlock
中。但是,ConnectedSystem大约应显示10个属性,我不想在视图中将XAML复制粘贴10次以上。相反,我想将XAML提取为UserControl,在其中可以将LabelText和Text设置为属性。 我尝试了此操作,但是我不确定如何使Caliburn.Micro自动将ConnectedSystem_Name传递到我的UserControl中。 这可能比在这里使用UserControl更好,因此,问题基本上是:将共享的XAML当作自己的控件,并且仍然能够使用Caliburn.Micros绑定的最佳方法是什么。     
已邀请:
Caliburn.Micro不能很好地将多个项目自动链接到单个控件。但是,您不必依赖自动绑定程序,可以改用普通的旧wpf绑定。
UserControl
是前往这里的方式。在代码中,您可以添加两个DependencyProperty,即
LabelText
Text
。 然后在您的UserControl的XAML中,绑定到新属性。 现在,您可以在使用此控件的地方在XAML中设置LabelText和Text值。因此,在主视图上添加控件,并将
LabelText
Text
绑定到ViewModel中的属性。     
您需要做的是在主视图中使用ѭ9来显示主视图模型的
ConnectedSystem
属性。通过使用ѭ9,您将被包括在视图模型绑定过程中,并且将应用视图模型绑定器规则。因此,您希望您的属性(使用Caliburn的默认实现)类型为
ConnectedSystemViewModel
,并具有名为
ConnectedSystemView
的视图。然后,在用于显示父级的视图中,您需要一个ѭ9,其中
x:Name
0(ConnectedSystemViewModel属性的名称。这将导致视图模型绑定程序将两者连接起来并完成其通常的工作。以下是一些代码来简化说明: ConnectedSystemView.xaml(在将“ 9”指定为显示主视图模型的已连接系统属性的控件时,约定将使用的用户控件)
<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;}
    }
}
然后在您的主视图中定义一个相对于view12ѭ类型的主视图模型属性命名的ContentControl。
<ContentControl x:Name=\"ConnectedSystem\"></ContentControl>
如果我理解正确的话,那么这应该是您所需要的所有默认的Caliburn.Micro约定。显然,您将在
ConnectedSystemViewModel
中添加10个
ConnectedSystem
属性,并在
ConnectedSystemView
中添加具有适当名称的适当控件以完成实现。 这样,您只需在主视图中定义一个ѭ9即可显示ConnectedSytem属性(而不是10个相同的自定义用户控件),并且约定将确定用于填充
ContentControl
的用户控件的类型。 通过约定将在插入到主视图
ContentControl
的content属性的
ConnectedSystemView
中,您具有要显示10个已连接系统属性的控件。     
如果只想显示或设置属性,我的方法是这样的:
public String ConnectedSystemName
{
    get { return _connectedSystem.Name; }
    set
    {
        _connectedSystem.Name = value;
        NotifyOfPropertyChange(() => _connectedSystem.Name);
    }
}
如果要显示或设置用户控件父级的属性,则可以创建附加属性以绑定到用户控件,即从用户控件获取/设置该属性     

要回复问题请先登录注册