Question

My Window A has DataContext A'and contains usercontrol B that gets DataContext B' in the usercontrol's loaded event.
And usercontrol B contains usercontrol C that gets DataContext C' in its loaded event.

No problems, except that in the output window during a debug session for usercontrol C several databinding errors are reported.
There is a short time where usercontrol has DataContext A' and B' before it gets set to C' because C inherits the DataContext from B (and B from A).

How should I avoid this behaviour, I would like to avoid the inheritance of the DataContext in this case.

Was it helpful?

Solution

In the declaration of the UserControl C set the DataContext to null, that way it won't inherit the DataContext from the parent and will not have a DataContext until you set it in the Loaded event...

<UserControl x:Class="MyNameSpace.MyUserControlC"            
        DataContext="{x:Null}>
    ...
</UserControl>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top