I have a relatively simple question that I do not understand. Suppose I have a ContentControl and a ContentTemplate specified and I would like to do the following:
<ContentControl ContentTemplate="{StaticResource UserView}" DataContext="{Binding User}" />
However to my surprise, this completely fails, while the following line works perfectly:
<ContentControl ContentTemplate="{StaticResource UserView}" Content="{Binding User}" />
Upon closer examination, the first line gives a surprising null
as its DataContext, whereas the latter one has the correct DataContext.
Could someone explain to me what is going on?
Thanks for everything!
I suppose that you bind to property User
that is defined is some ViewModel
. Than you don't have to set DataContext
:
<ContentControl ContentTemplate="{StaticResource UserView}" Content="{Binding}" DataContext="{Binding User}" />
And is more common write this:
<ContentControl ContentTemplate="{StaticResource UserView}" Content="{Binding User}"/>