wpfbindingrelativesourcefindancestor

Styles and DataTemplates: FindAncestor-like search including Self


I have a style containing binds expression of the form

{Binding Path, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MyType}}}

However, sometimes the DataContext in which this style operates is actually of type MyType. In this case, FindAncestor doesn't do what I want: it starts its search from the parent.

Is there any {Binding ...} incantation that checks the type of the current datacontext before continuing up the element hierarchy? I have other solutions specific to my project, but FindAncestor is so close...


Solution

  • RelativeSource bindings do not look for DataContexts, they search the visual tree for UI elements. The AncestorType should be a control.

    To perform a RelativeSource binding on the same control you can use RelativeSource Self