2016-10-24 64 views
-1

我在同一个视图中有两个类(模型)和两个文本框。第一个文本框应该绑定到第一个模型,第二个文本框应该绑定到第二个模型。我彻底搜查了它。我需要一个MVVM模式的例子。将数据从多个模型绑定到单个视图

+0

发布您到目前为止尝试过的东西。 –

+0

@Ayyappan Subbramanian。我发布了我做的答案。请对此发表评论。 –

+0

向下投票导致我被该网站阻止发布新问题。朋友首先请在downvoting之前的理由,以便我可以更正 –

回答

1

我的模型

public class Customer 
{ 

    public int CustomerId { get; set; } 
    public string Title { get; set; } 
    public string Name { get; set; } 
    public string Address1 { get; set; } 
    public string Address2 { get; set; } 
    public string Area { get; set; } 
    public int MobileNumber { get; set; } 
} 
public class Account 
{ 
    public int AccountId { get; set; } 
    public string AccountType { get; set; } 
} 

我的视图模型

public class TaskManagerVM 
{ 
    private Customer CustomerObj = new Customer(); 
    private Account AccountObj = new Account(); 
    public int CustomerId 
    { 
     get { return CustomerObj.CustomerId; } 
     set { CustomerObj.CustomerId = value; } 
    } 
    public string Name 
    { 
     get { return CustomerObj.Name; } 
     set { CustomerObj.Name = value; } 
    } 
    public string Address1 
    { 
     get { return CustomerObj.Address1; } 
     set { CustomerObj.Address1 = value; } 
    } 
    public string Address2 
    { 
     get { return CustomerObj.Address2; } 
     set { CustomerObj.Address2 = value; } 
    } 
    public int AccountId 
    { 
     get { return AccountObj.AccountId; } 
     set { AccountObj.AccountId = value; } 
    } 
    public string AccountType 
    { 
     get { return AccountObj.AccountType; } 
     set { AccountObj.AccountType = value; } 
    } 


} 

我绑定第一个文本框来查看模型的客户ID属性,它连接到第一个模型,customer.cs 我绑定第二个文本框查看连接到第二个模型的模型的Account ID属性,account.cs

它是否正确?

+0

看不到任何错误。你在正确的道路上。 ViewModel可以从多个来源或模型中获取数据,并可以查看视频.. –

+0

你做得很好。让我知道你在哪里被击中.. –

+0

感谢您的评论朋友。我首先不知道并提出了这个问题。然后我想象这个解决方案。 –

相关问题