2017-10-05 67 views
2

我正在用MVVM開發一個wpf應用程序。如何在viewmodel中封裝模型並在datagrid中使用它?

現在,我有我的模型實體,每個視圖和當然視圖一個viewmodel。

還有就是我的實體的例子:

public class Group : INotifyPropertyChanged 
{ 
    public string GID { get; set; } 
    public string Label { get; set; } 
    public DateTime Date { get; set; } 
    public int Rank { get; set; } 
} 

public class Person : INotifyPropertyChanged 
{ 
    public string Name { get; set; } 
    public Group Group { get; set; } 
} 

(我刪除通知簡化)

目前,我映射我的模型實體視圖模型throught其中重複模式的特性的中間類,並添加一些其他。總結:型號< - >組件< - > viewmodel。但是這爲我增加了更多無用的複雜性。

另外我有一個特殊的情況,我想顯示一個人在一個數據網格列表。

You can see here in excel

正如你所看到的,組列是開始日期和結束日期和級別列過濾下拉是基於先前的下拉列表中選擇組過濾。

所以,我的問題是:你如何用viewmodel包裝你的模型?和這種情況下特別。

+0

您可以簡單地從模型類派生視圖模型類。 – Clemens

+0

謝謝你的回答,但我試過這種方式,我是這樣的類:'class PersonViewModel:Person {}'和class GroupViewModel:Group {}'但我不得不隱藏Person的組屬性GroupViewModel屬性。 所以,我不確定這是最好的方式,或者我錯了某處:) –

+0

我無法看到圖像,因爲我在代理之後。如果從模型類派生組件類,然後公開視圖模型中的組件集合以將它們綁定到視圖中,會怎麼樣? – XAMlMAX

回答

1

最後,我發現一個簡單的解決方案,基於this article我同事與我分享。

public class PersonViewModel(){ 
    public Person Person { get; set; } 
} 

public class MainViewModel(){ 
    public Club Club { get; set; } 
    public ObservableCollection<PersonViewModel> PersonViewModels { get; set;} 
    public PersonViewModel CurrentPersonViewModel { get; set; } 
} 

這可能並不完美,但是這樣可以避免我的解決方案過於複雜。

相關問題