Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grids with ViewModels - WPF

Sorry if this has already been asked, but I just want to make sure that I'm doing this right.

If I have a domian object that has say 10 properties on it. I have a grid on my main form that I want to show the pretty much all the the properties from the model.

I created a viewmodel to wrap the domain object to show in the gridview but then I have to expose all the properties again. I just feel binding straight against the model through the viewmodel feels dirty and defects the purpose a bit.

So for example I don't really like this:

 {Binding DomainObject.Property}

where DomainObject is property on my view model.

So my main question is, should I expose all the properties on the model through the view model just to bind it to the grid?

EDIT: Just for added information the domian objects are LINQ-To-SQL objects, so I don't think they implement INotifyPropertyChanged but I'm not sure.

like image 458
Nathan W Avatar asked Mar 19 '26 01:03

Nathan W


1 Answers

Some people will say it doesn't matter, others say it does. I'm in the latter camp, for these reasons:

  • You increase the dependencies of the view, as it now depends on the data model, not just the view model.
  • You require the designers need to know the properties and structure of your data model.
  • You create more work for the (almost inevitable) refactoring when you decide you need a layer of indirection for formatting, validation, or whatever it might be.
  • As Thomas pointed out, data models often don't implement change notification

Yes, it's a little more work, but I believe it's worth it to reduce decoupling, maintenance headaches, collaboration with designers, and correctness.

PS. If you find yourself in this situation a lot, you might consider an implementation of ICustomTypeDescriptor that wraps any data object and exposes its properties with change notification. That way your VM can extend this generic wrapper until you decide you need to pull properties out for purposes such as formatting and validation.

like image 173
Kent Boogaart Avatar answered Mar 22 '26 20:03

Kent Boogaart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!