将自定义用户控件绑定到区域上下文的属性



我正在通过编写一个小型人力资源应用程序来学习Prism(ver.5)框架。

My EmployeeSummaryView是一个简单的主详细信息页面,它使用选项卡控件来更好地组织Person类型的属性。此选项卡控件有一个区域上下文绑定到CurrentEmployee。

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthday { get; set; }
    public string PhoneNumber { get; set; }
    public Address MailingAddress { get; set; }
    public Person EmergencyContact { get; set; }
}

我在"联系信息"选项卡上遇到了困难。在这里,我使用自定义用户控件来编辑邮寄地址。我建立绑定的第一本能是执行以下操作:

<local:EditAddressView DataContext="{Binding CurrentEmployee.MailingAddress}"/>

但我对Prism和MVVM了解得越多,它就越有味道。

将自定义控件绑定到RegionContext上的属性的正确方法是什么?

通过绑定到Model而不是ViewModel,您将陷入困境。使用ViewModels包装模型,然后绑定到这些模型。

public class PersonViewModel
{
    public PersonViewModel(Person person)
    {
       ...
       MailingAddress = new AddressViewModel(person.address);
    }
    public AddressViewModel MailingAddress { get; private set; }
}

最新更新