5

我对 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 绑定。

4

3 回答 3

5

您需要做的是ContentControl在主视图中使用 a 来显示ConnectedSystem主视图模型的属性。通过使用 aContentControl您将被包含在视图模型绑定过程中,并且将应用视图模型绑定器规则。因此,您希望您的属性(使用 Caliburn 的默认实现)具有类型ConnectedSystemViewModel并具有名为ConnectedSystemView. 然后在用于显示您想要的父视图的视图ContentControlx:NameConnectedSystemConnectedSystemViewModel 属性的名称。这将导致视图模型绑定器连接两者并执行其通常的工作。为了清楚起见,这里有一些代码:

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 个连接的系统属性的控件。

于 2011-05-16T23:05:22.203 回答
3

Caliburn.Micro 不能很好地自动将多个项目链接到单个控件。但是,您不必依赖自动活页夹,您可以改用普通的旧 wpf 绑定。

AUserControl是去这里的路。在代码中,您可以添加两个 DependencyPropertiesLabelTextText.

然后在您的 UserControl 的 XAML 中,绑定到新属性。

在您使用此控件的位置,您现在可以在 XAML 中设置 LabelText 和 Text 值。因此,在您的主视图上添加控件,并将其绑定LabelTextText您的 ViewModel 中的属性。

于 2011-05-16T07:29:31.013 回答
0

如果只想显示或设置属性,我的做法是这样的:

public String ConnectedSystemName
{
    get { return _connectedSystem.Name; }
    set
    {
        _connectedSystem.Name = value;
        NotifyOfPropertyChange(() => _connectedSystem.Name);
    }
}

如果要显示或设置用户控件父级的属性,可以创建附加属性以绑定到用户控件,即从用户控件获取/设置属性

于 2012-07-06T09:09:08.413 回答