【发布时间】:2013-01-14 10:34:48
【问题描述】:
我正在使用来自Josh Smith 的示例。他有一个显示 CustomerViewModel 列表的 WorkspaceViewModel。他使用相同的 ViewModel 来显示所有客户和编辑一个客户。
这是一个好习惯吗?如果我有一个 CustomerViewModels 列表,我不需要 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 中我可以忽略这两个命令,但这是一个好的设计吗?
【问题讨论】:
-
从您的选项 a 中选择一个,在选项 b 中,客户是模型而不是视图模型,在 c 中通过继承,您需要按视图划分视图模型。
-
如果是 Josh Smith,那么是的,这是一个“好习惯”。