4

Being new to both WPF and MVVM, I'm studying Josh Smith's article on the MVVM pattern and the accompanying sample code.

I can see that the application is started in app.xaml.cs by constructing a MainWindow object, wiring it to a MainWindowViewModel object and then showing the main window. So far so good.

However, I can't find any code which instantiates the AllCustomersView or CustomerView classes. Using "find all references" on the constructors of those views comes up with nothing. What am I missing here?

Wim Coenen
  • 66,094
  • 13
  • 157
  • 251

1 Answers1

6

WPF's DataTemplate is doing the magic. For example when you set content of a Contentcontrol with an instance of CustomerViewModel with the below DataTemplate in your resource dictionary (usually in app.xaml). Then you are gonna see CustomerView usercontrol coming up in the UI.

<DataTemplate DataType="{x:Type vm:CustomerViewModel}">
  <vw:CustomerView />
</DataTemplate>
Jobi Joy
  • 49,102
  • 20
  • 108
  • 119
  • Ah thanks, I found the mapping in `MainWindowResources.xaml` now. – Wim Coenen Mar 13 '10 at 02:04
  • Hi Jobi, I know its an old post, but I have related question, so I will be glad to get your opinion: I managed to understand that every time we set the a content property to xxxViewModel instance, an xxxView object will be rendered. But I couldnt find where does he actually SET the content property in the code...? – ET. Nov 08 '10 at 08:05