2016-10-24 61 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

感謝您的評論朋友。我首先不知道並提出了這個問題。然後我想象這個解決方案。 –

相關問題