Polymorphic DataBinding with MVVM
I'm new to SL and I was wondering what are the best practices to solve my problem. I have a base class called say Car which has开发者_运维技巧 PropertyA, PropertyB etc. I derived a Ferrari from Car and added PropertyC. I also derived Lada (:D) from the car base class and added PropertyD.
On client side, now I have a collection of car objects which I want to bind to a control in my view. Some of the objects in the collection is Ferrari, and some are Lada. I want a custom DataTemplate for each type. So what's your suggestions? Where should check the type of the car object and how should I assign it a custom view? Should I use a ValueConverter or is there a more clean way to solve the problem? I have no experience in MVVM, so there's some things left dark for me.
Thanks
You should use a DataTemplateSelector . Here is a tutorial : http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector
The same thing for silverlight (although I havent tested it) :) http://www.codeproject.com/KB/silverlight/SLTemplateSelector.aspx
精彩评论