以两种方式绑定两个视图模型中的属性

发布于 2024-11-05 01:39:52 字数 2381 浏览 4 评论 0原文

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

 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 属性。这可能吗?我是否走在正确的轨道上,或者有更好的方法吗?

谢谢,

罗兰

I'm starting Caliburn Micro development and I have thought of an architecture where a viewmodel has properties, injected by MEF, which are other viewmodels. That way I can use contentcontrols in the view to position them the way I want.

 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);
        }
    }

The viewmodel of SingleLineTextContentItem looks like this:

[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);
        }
    }
}

Now, I need a way to bind the NameCaption property to the Caption property in a two-way manner. Is that possible? I'm I on the right track with this or is there a better way to do this?

Thanks,

Roland

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2024-11-12 01:39:52

我所做的不是让支持字段路由到其他视图模型

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

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

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 中引发事件code> 属性并在处理程序中。

What I do is instead of having a backing field just route to the other view model

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

However if the Caption property on the ISingleLineTextContentItem can get set independently then you need to register changes on the event and have the view model listen to changes. So instead you need somthing along the lines of:

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);
}

Since either the Caption property or the FirstName property can change then we need to raise the event in the FirstName property and in the handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文