2

So essentially I'm making a WPF/MVVM Light application and I currently have a TreeView that represents a variety of different types of objects. Each of these objects is wrapped in a very generic "ViewModel" that currently just exposes their name to the TreeView display in the application.

Linked conceptually to this tree, I want to provide an Object Viewer below the tree, such that when a user selects an item in the three, the object viewer is populated with the Properties of that node and it allows the user to change and save new values to the node in question.

I'm effectively trying to create an abstraction that can take a variety of types (7 different object types) and expose their Properties AND allow the user to edit them. Essentially, I can bind the properties of this abstraction to a group of Text/Display boxes on the UI, and when the user hits save, have it call update methods on the actual underlying data objects from this middle wrapper class.

Currently, the only way I can think to accomplish this is to make a separate wrapper for each underlying object type (since they all have different Properties), and essentially hard-code the fields and update methods.

Are there any other options in terms of providing further abstraction and creating a general wrapper class capable of exposing and updating Properties from a variety of objects? Thanks.

TSM
  • 582
  • 2
  • 10
  • 25

1 Answers1

0

Instead of wrapping every model in a different ViewModel, you may want to expose the model directly to the View and create a DataTemplate for each type of model, this will allow you to have different UIs for each model type without having to place an intermediate ViewModel in between. Just a suggestion.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • I'm rather new to WPF and MVVM. Would you be able to specify a DataTemplate in a way that describes how to display an object in, say, 10 Text Boxes and 10 TextBlocks? If so, do you know of any good resources for understanding DataTemplates? I have a lot of reading material I'm trying to get through on wpf, but I'm also under some time constraints for an early demo. Thanks! – TSM Jun 13 '13 at 15:00
  • I'm looking at this example and it appears as if it is possible: http://stackoverflow.com/questions/5706500/are-data-templates-obsolete-in-mvvm – TSM Jun 13 '13 at 15:05
  • @TSM Exactly. DataTemplates are meant to define "how to show a specific object type". The answer you linked perfectly demonstrates what I'm saying – Federico Berasategui Jun 13 '13 at 15:12
  • So, after some reading, I think I understand how to define the Data Templates for each of those objects. Now, I also understand how I could use that template statically in my UI. My one last question is how can I dynamically choose my DataTemplate in the XAML based on the data type of a bound element? – TSM Jun 13 '13 at 16:27
  • @TSM Just put a `ContentPresenter Content="{Binding SomeModel}"`, making sure that you can put diffent Model types in that property. WPF will take care of searching the appropiate `DataTemplate` within the `Resources` you defined and use it to render that specific Model. Also make sure that you `NotifyPropertyChange("SomeModel");` – Federico Berasategui Jun 13 '13 at 18:02