简体   繁体   English

WPF:MVVM和编辑分层数据

[英]WPF: MVVM & Editing Hierarchical Data

I'm still trying to wrap my head around MVVM. 我仍然试图围绕MVVM。 Let's say I have a Model that looks something like this: 假设我有一个看起来像这样的模型:

public class Company  
{  
    public IList<Division> Divisions { get;set;}  
}

public class Division  
{  
    public string Name { get;set;}
    public IList<Department> Departments { get;set}
}  

public class Department
{
    public string UnitNumber { get;set;}
    public string DepartmentName { get;set;}
    public IList<Employee> Employees { get;set;}
}

public class Employee
{
    public string FirstName { get;set;}
    public string LastName { get;set;}
    public string JobTitle { get;set;}
}

Now let's say I want to display the company hierarchy in a hierarchical grid, I create a CompanyViewModel class below: 现在假设我想在分层网格中显示公司层次结构,我在下面创建了一个CompanyViewModel类:

public class CompanyViewModel : INotifyPropertyChanged
{
    private Company _company;
    public Company Company
    {
         get { return _company;}
         set { _company = value; NotifyPropertyChanged("Company");}
    }
}

Now on my view (WPF), I would set the data context to the ViewModel and the datagrid of choice would bind to the "Company" Path. 现在在我的视图(WPF)上,我将数据上下文设置为ViewModel,并且选择的datagrid将绑定到“Company”路径。 Everything's great so far.... I get a nice expand/collapse interface into Divions, departments, employees... 到目前为止一切都很棒....我在Divions,部门,员工中得到了一个很好的扩展/折叠界面......

Except: 除了:

  1. What if the grid is editable... Department Names should be able to be changed (and validated by the ViewModel, same for Employee names.. 如果网格是可编辑的,那么...部门名称应该能够被更改(并由ViewModel验证,对于员工名称也是如此)。

  2. What if I want to add new employees, departments, etc. all of that should be reflected in the grid without rebinding (that's the point of WPF databinding isn't it?) 如果我想添加新的员工,部门等,那么所有这些应该反映在网格中而不重新绑定(这就是WPF数据绑定的重点不是吗?)

Potential Solution: 潜在解决方案

You have a separate ViewModel class for every Domain class... 每个Domain类都有一个单独的ViewModel类...

This seems to imply alot of mapping from DTO -> ViewModel, duplication (because they're almost the same objects and yet not exactly.) Given that I'm probably already mapping from some kind of ORM entity -> DTO on the service side, sending it over the wire (WCF) to the client, mapping every DTO hierarchy into its own ViewModel is a heavy, expensive process (not to mention the work involved in doing so.) 这似乎意味着很多来自DTO的映射 - > ViewModel,重复(因为它们几乎是相同的对象,但并不完全相同。)鉴于我可能已经从某种ORM实体映射 - >服务端的DTO ,通过线路(WCF)将其发送到客户端,将每个DTO层次结构映射到它自己的ViewModel是一个繁重而昂贵的过程(更不用说这样做所涉及的工作。)

Bleeding things like INotifyPropertyChanged, ObservableCollection, etc into my DTOs seems like a hack. 将INotifyPropertyChanged,ObservableCollection等内容放到我的DTO中似乎是一种黑客行为。

Does anyone have a better solution? 有没有人有更好的解决方案? Am I crazy? 我疯了吗? ;-) ;-)

"Bleeding things like INotifyPropertyChanged, ObservableCollection, etc into my DTOs seems like a hack." “将INotifyPropertyChanged,ObservableCollection等内容放到我的DTO中似乎是一种黑客行为。”

I feel your pain, but that is the approach I've taken on a couple of projects. 我感觉到你的痛苦,但这是我在几个项目中采取的方法。

Bottom line: For WPF databinding to "work" as advertised the objects/collections you bind to need to support INotifyPropertyChanged and ObservableCollection. 结论:对于WPF数据绑定到“工作”,如您所宣传的那样,您绑定的对象/集合需要支持INotifyPropertyChanged和ObservableCollection。

Personally I think creating DTO's that support this is a LOT less work than constantly translating data back and forth into a viewmodel or other intermediate object that is essentially a richer version of the DTO object. 我个人认为创建支持这一点的DTO比不断地将数据来回转换为视图模型或其他中间对象(本质上是DTO对象的更丰富版本)的工作少得多。

如果您厌倦了输入INotifyPropertyChanged的代码而且还使用了自动属性,为什么不看看MoXAML Power Toys ,它允许您将自动属性转换为可通知的属性?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM