2

我有一个名为CompanyListViewModel. 它代表我的应用程序中的公司列表。它有一个类型ObservableCollection<CompanyViewModel>为 ListView 的属性。

CompanyListViewModel有一个命令叫做OpenCommand. 此命令在新窗口中打开选定的公司进行编辑。

调用的函数OpenCommand如下所示:

public void Open()
{
    Company selectedCompany = SelectedCompanyViewModel.Model
    CompanyViewModel elm = new CompanyViewModel(selectedCompany);
    openHandler(elm); // opens a new window with the given vm.
}

所以这个函数:

  • 获取当前选中的CompanyViewModel
  • 检索基础模型。
  • 实例化一个共享现有视图模型模型的新视图模型。
  • 打开此视图模型。

这个对吗?我有两种选择:

  • 使用现有视图模型打开一个新窗口
  • 从数据库中获取一个新模型,将它放在一个新的视图模型中,然后打开一个窗口。

我应该使用哪种方法?

4

1 回答 1

2

That depends on the functionality your application should have. When changes made to the model have to be present in all where other places the model is used, using the same model seems to be the correct way.

When you want all places the view model is used to immediately show any changes made to the model even if they aren't confirmed by the user you should share your view model.

Opening the new window with a completely different model is an alternative when you want no logical connection between the both instances. Then you have to rely on your business logic that the first model gets replaced when the second one was changed and saved back into database.

于 2012-04-26T11:15:53.323 回答