How to access DataContext of parent in wpf when parent type can be different?

binding, datacontext, wpf, xaml

Solution

Remember that if `DataContext` is not explicitly set, it will inherit its parent's `DataContext`. If, for some reason, this doesn't work, you should take a look at binding with `RelativeSource`.

Something like this might work:

<Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext}}"

Given that the `Grid` has an ancestor of type `Window` (which I think all controls should have).

Problem

I need to access DataContext of parent in wpf xaml. The whole xaml page code is dynamic. So don't know about the type of parent. I am writing this ``` <Grid DataContext={Binding Path=.}> ``` Is this correct?

Original source

Related problems