Binding to ViewModel properties from elements with another DataContext

Topics: Issues / bugs
Aug 23, 2012 at 10:34 AM


I have a usercontrol which binds its DataContext to a property of the Model (Model.Details).
Catel creates an instance of DetailView with DetailViewModel as DataContext and injected Model.Details as new Model for this VM.

The DetailView Control also has a Caption property which has to bind to a property of the MainViewModel.

Because the local DataContext of DetailView is changed, the simple binding does not work.
Instead I gave the MainViews UserControl a name (control) and referenced its
DataContext within the binding using ElementName="control". 

It seems, the binding of the property takes place before Catel changes the DataContext from the Model to the ViewModel.
VS writes that it tried to bind to DetailsModel.ViewModelProperty instead to MainView.ViewModelProperty.

I've created a simple sample showing the effect.
You can download it here: 

 I would be glad if you can help me out with this issue.

<catel:UserControl x:Class="Views.MainView"
    <views:DetailView Caption="{Binding Path=DataContext.ViewModelProperty, ElementName=control}" 
                      DataContext="{Binding Path=Model.Details}" />

Aug 24, 2012 at 11:19 AM

Give me some time to figure this out. I also have to take a look at the nested stackoverflow stuff. And of course I have other things to do ;)