Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于显示和编辑的WPF MVVM单视图模型_Wpf_Mvvm_Viewmodel - Fatal编程技术网

用于显示和编辑的WPF MVVM单视图模型

用于显示和编辑的WPF MVVM单视图模型,wpf,mvvm,viewmodel,Wpf,Mvvm,Viewmodel,我使用的例子来自。他有一个WorkspaceViewModel,其中显示CustomServiceWModel的列表。他使用相同的ViewModel来显示所有客户和编辑单个客户 这是一种好的做法吗?如果我有一个CustomerViewModels列表,我不需要SaveCommand或CloseCommand或一些IsSelected标志 使用单独的EditCustomerServiceWModel是否更好?但是如何处理与工作区相关的东西呢?例如: public class Workspace :

我使用的例子来自。他有一个WorkspaceViewModel,其中显示CustomServiceWModel的列表。他使用相同的ViewModel来显示所有客户和编辑单个客户

这是一种好的做法吗?如果我有一个CustomerViewModels列表,我不需要SaveCommand或CloseCommand或一些IsSelected标志

使用单独的EditCustomerServiceWModel是否更好?但是如何处理与工作区相关的东西呢?例如:

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;    
}
编辑:
我试图澄清我的问题。在Josh Smith示例中的CustomerServiceWModel中,他有关闭和保存客户的命令。在AllCustomerView中,他有一个GridView,它绑定到CustomerViewModels的ObservableCollection。但在GridView中,这两个命令都不是必需的。在GridView中,我可以忽略这两个命令,但这是一个好的设计吗

我不太清楚你的意思,因为快速浏览那篇文章可以看出,他正在使用列表和视图/编辑视图模式,分别称为
AllCustomers-ViewModel
CustomerViewModel
。这当然是推荐的做法,而不是使用具有多个职责的单一视图模型


这两个视图模型都继承自
WorkspaceViewModel
,后者添加了他的工作区功能。总之,如果你想构建类似的东西,我会遵循他的模式。你也应该认真考虑一个MVVM框架,比如增加简单的基于约定的视图构图和屏幕生命周期。

从选项A中选择一个,在选项B中,CouthUME是一个模型而不是VIEW模型,在C继承中,你需要用视图划分视图模型。如果它是乔什史密斯,那么,是的。“良好做法”。
// 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;    
}