tags:

views:

316

answers:

2
+2  A: 

You should read this article by Josh Smith, everything will seem much clearer afterwards...

Thomas Levesque
+1  A: 

It may not need to specifically set the DataContext if the contacts are being shown in a listview or something similar.

If the datacontext of the listview is set to the Contacts property of the MainViewModel, then each item's datacontext will be set automatically to the specific ContactViewModel object, which may trigger the items to be presented using a ContactView control, assuming that certain template bindings were set up earlier in the document.


Sorry, I was having problems with codeplex and have only just managed to get the document to download. The block of code just before the diagram confirms my suspicion:

<Grid>
    <ListBox ItemsSource="{Binding Contacts}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <views:ContactView />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

Since the ListBox is bound the the Contacts ObservableCollection each ListItem will have its DataContext set to the specific object that it is bound to. The DataTemplate is set up to show each item as a ContactView control. Therefore the ContactView's DataContext will be set to the right Contact object from the collection, all of this happens behind the scenes without you needing to actually set the property yourself.

Martin Harris