Confusion with MVVM WPF

I downloaded the MVVM doc from CodePlex but I don't understand this diagram.

alt text http://img194.imageshack.us/img194/3959/diagram.png

In the doc, the ContactView never sets its DataContext to the ContactViewModel, so I don't understand why this diagram showed that the ContactView was referring to the ContactViewModel via the DataContext.

I don't know when it sets the ContactView.DataContext or the document doesn't have this point?

0


a source to share


2 answers


You must read this article by Josh Smith, after that things will become much clearer ...



+2


a source


It may not be necessary to specifically set the DataContext if the contacts appear in the list or something similar.

If the list datacontext specifies the Contacts property MainViewModel, each datacontext item will automatically be set to a specific ContactViewModel object, which can initiate items to be presented using the ContactView control, provided that a specific binding template has been set earlier in the document.




Sorry I had problems with the codeplex and just managed to download this document. The block of code just before the diagram confirms my suspicions:

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

      

Since the ListBox is bound, the ObservableCollection for the Contacts ListItem will have its DataContext for the specific object it is bound to. The DataTemplate is configured to display each item as a ContactView control. Therefore the ContactView DataContext will be set to the right of the Contact object from the collection, all of this happens behind the scenes, unless you really need to set the property yourself.

+1


a source







All Articles