View within DataTemplate

Nov 23, 2012 at 6:24 AM

Hi Ian,

first i want to thank you for this graet work.
But it would be much easier to work with StyleMVVM if the
source code would be well documented.


Now my question.

I have an ItemsControl (GridView) and in the ItemTemplate's DataTemplate 
i want to use a View mith a ViewModel. If i use your View-ViewModel Connection the
ViewModel is correctly created and set as DataContext. But now the prior DataContext
which is an Item from the ItemsControl is lost. I think it should be a possibility to inject
the prior DataContext to the ViewModel.

What do you think? 

Best regards, Uli

Nov 23, 2012 at 5:35 PM

Hi Uli,

I do agree more documentation is required and currently I'm working on more external documentation (under the documentation section here on codeplex) with the intention of adding more XML documentation in the future.

So on to your question, that's an interesting use case and one I hadn't really thought of but still in certain cases very practical.

What if I add an interface to the framework that has one property "object ParentDataContext { get; set; }". When your viewmodel implements this interface the parent data context will be injected into the viewmodel when the view is inserted into the ViewTree. This will solve your problem as well as other more generic use cases that don't use a data template.

I can add this in for the next release if you can wait a week or two. I had planned to release it earlier but the changes for Windows Phone 8 are more substantial than I had hoped and its taking me a little longer to implement.

Hope this helps.



Nov 26, 2012 at 5:40 AM

That will be perfect.

Thank you.

Nov 26, 2012 at 9:16 AM

Hi Ian,

i tested it with the current beta.
In my case it doesn't work because in my DataTemplate the View(UserControl) is located within an Grid.
So in the ViewModelParentDataContextBinder the DataContext from the Grid is injected, but this is null.
In my case it should be searched up until the ContentPresenter which holds the right DataContext.
But i'm not sure if this would be proper solution for all cases.

Best regards, Uli


Nov 26, 2012 at 1:41 PM

Hi Uli,

Lets move this discussion to email because I have some questions about your data template (I tested mine with a Grid and it works so I'd like to figure out what the difference is and why its not working for you).

My email is my codeplex username at