Search code examples
wpfxamladornerlayer

Is there always an Adorner Layer between a parent content control and its children?


Just trying to clarify an observation. (I can't seem to find a straight answer from Google). I am doing some image editing using adorners. My images are both in an ItemsControl and as children of an InkCanvas.

When using GetAdornerLayer() for an element within the ItemsControl, I automatically obtain an Adorner Layer over the element within the ItemsTemplate. But if I go further up the visual tree with VisualTreeHelper, I find another AdornerLayer above all the items of the ItemsControl. (The ItemsControl is itself a child of a Grid). On the other hand, when accessing the AdornerLayer for a child of the InkCanvas, I get an adorner layer that lies between the InkCanvas itself and its children.

Hence, in both cases, it seems apparent that WPF is always placing an adornerlayer between a contentcontrol and its children.

Is this indeed the case?

TIA.


Solution

  • It depends on how the ControlTemplates of the controls are defined. The AdornerLayer.GetAdornerLayer method traverses up the visual tree starting at the specified Visual and returns the adorner layer of the first AdornerDecorator or ScrollContentPresenter element it finds.

    So WPF doesn't automatically "always placing an adornerlayer between a contentcontrol and its children". It is rather the control author that provides the adorner layer by adding an AdornerDecorator to the visual tree.

    If you for example look at the default ControlTemplate for the Window, you will see that it indeed includes an <AdornerDecorator> element.