2013-01-14 26 views
1

我使用的示例來自Josh Smith。他有一個WorkspaceViewModel,顯示CustomerViewModel的列表。他使用相同的ViewModel來顯示所有客戶並編輯單個客戶。用於顯示和編輯的WPF MVVM Single ViewModel

這是一個很好的做法嗎?如果我有一個CustomerViewModel的列表,我不需要SaveCommand或CloseCommand或一些IsSelected標誌。

單獨的EditCustomerViewModel更好嗎?但如何處理Workspace相關的東西?例如:

public class Workspace : ViewModel 
{ 
    public ICommand CloseCommand; 
} 

public class AllCustomers : Workspace 
{ 
    public ObservableCollection<CustomerViewModel> CustomerViewModels; 
} 

// Option A (one ViewModel for display and edit): 
public class CustomerViewModel : ? 
{ 
    public string CustomerName; 
    public ICommand SaveCommand;  
} 

或者分拆:

// Option B: 
public class CustomerViewModel : ViewModel 
{ 
    public string CustomerName; 
} 

public class EditCustomerViewModel : Workspace 
{ 
    public CustomerViewModel CustomerViewModel; 
    public ICommand SaveCommand; 
} 

// Option C (CustomerViewModel does not need CloseCommand but EditCustomerViewModel does): 
public class CustomerViewModel : Workspace 
{ 
    public string CustomerName; 
} 

public class EditCustomerViewModel : CustomerViewModel 
{ 
    public ICommand SaveCommand;  
} 

編輯: 我試圖澄清我的問題。在Josh Smith的例子中的CustomerViewModel中,他有關閉和保存客戶的命令。在AllCustomerView中,他有一個綁定到CustomerViewModels的ObservableCollection的GridView。但在GridView中,這兩個命令都不是必需的。在GridView中,我可以忽略這兩個命令,但這是一個好的設計嗎?

+0

from your options a是一個可供選擇的選項,在選項b中,costumer是一個模型,而不是視圖模型,而c是通過繼承,您需要將視圖模型按視圖劃分。 – ZSH

+0

如果是喬什史密斯,那麼是的,這是一個「良好的習慣」。 –

回答

0

我不太清楚你的意思,快速看看那篇文章顯示他正在使用列表和視圖/編輯視圖模態,分別稱爲AllCustomers­ViewModelCustomerViewModel。這當然是推薦的做法,而不是使用具有多項責任的單一視圖模型。

這兩種視圖模型都從WorkspaceViewModel繼承,這些模型添加了他的工作區功能。所以,總之,如果你想建立類似的東西,我會遵循他的模式。你還應該認真考慮一個MVVM框架,比如Caliburn.Micro,它增加了基於常規的視圖組合以及屏幕生命週期。