0

我正在开始 Caliburn Micro 开发,并且我想到了一种架构,其中视图模型具有由 MEF 注入的属性,这些属性是其他视图模型。这样我就可以在视图中使用 contentcontrols 来按我想要的方式定位它们。

 public class ContactsProfileViewModel : Conductor<IContentItem>, IContactsModuleViewModel,      IModule, IPartImportsSatisfiedNotification
{
    private string name;
    private string nameCaption;
    private ISingleLineTextContentItem firstName;
    private ISingleLineTextContentItem lastName;

    public ContactsProfileViewModel()
    {
        this.DisplayName = "Contact Tab";
    }


    public string Name
    { 
        get
        {
            return this.name;
        }
        set
        {
            this.name = value;
            this.NotifyOfPropertyChange(() => Name);
        }
    }
    public string NameCaption 
    { 
        get
        {
            return this.nameCaption;
        }
        set
        {
            this.nameCaption = value;
            this.NotifyOfPropertyChange(() => NameCaption);
        }
    }

    [Import(typeof(ISingleLineTextContentItem))]
    public ISingleLineTextContentItem FirstName
    {
        get { return this.firstName; }
        set 
        { 
            this.firstName = value;
            this.NotifyOfPropertyChange(() => FirstName);
        }
    }

    [Import(typeof(ISingleLineTextContentItem))]
    public ISingleLineTextContentItem LastName
    {
        get { return this.lastName; }
        set
        {
            this.lastName = value;
            this.NotifyOfPropertyChange(() => LastName);
        }
    }

SingleLineTextContentItem 的视图模型如下所示:

[Export(typeof(ISingleLineTextContentItem))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class SingleLineTextContentItemViewModel : PropertyChangedBase, ISingleLineTextContentItem
{
    private string textBoxText;
    private string caption;

    public string TextBoxText
    {
        get { return textBoxText; }
        set 
        { 
            textBoxText = value;
            this.NotifyOfPropertyChange(() => TextBoxText);
        }
    }

    public string Caption
    {
        get { return caption; }
        set 
        {
            this.caption = value;
            this.NotifyOfPropertyChange(() => Caption);
        }
    }
}

现在,我需要一种以双向方式将 NameCaption 属性绑定到 Caption 属性的方法。那可能吗?我是在正确的轨道上还是有更好的方法来做到这一点?

谢谢,

罗兰

4

1 回答 1

1

我所做的不是让支持字段路由到另一个视图模型

public string NameCaption 
{ 
    get
    {
        return FirstName.Caption;
    }
    set
    {
        FirstName.Caption = value;
        this.NotifyOfPropertyChange(() => NameCaption);
    }
}

但是,如果 上的Caption属性ISingleLineTextContentItem可以独立设置,那么您需要注册事件的更改并让视图模型监听更改。因此,您需要以下内容:

public string NameCaption 
{ 
    get
    {
        return FirstName == null ? string.Empty : FirstName.Caption;
    }
    set
    {
        if(FirstName != null)
           FirstName.Caption = value;
    }
}

[Import(typeof(ISingleLineTextContentItem))]
public ISingleLineTextContentItem FirstName
{
    get { return this.firstName; }
    set 
    { 
        if(this.FirstName != null)
            this.FirstName.PropertyChanged -= FirstNameChanged;

        this.firstName = value;

        if(this.FirstName != null) 
           this.FirstName.PropertyChanged += FirstNameChanged;

        this.NotifyOfPropertyChange(() => FirstName);
        this.NotifyOfPropertyChange(() => NameCaption);
    }
}

private void FirstNameChanged(object sender, PropertyChangedEventArgs e)
{
    if(e.PropertName == "Caption")
        this.NotifyOfPropertyChange(() => NameCaption);
}

由于Caption属性或FirstName属性都可以更改,因此我们需要在FirstName属性和处理程序中引发事件。

于 2011-05-04T12:15:05.237 回答