Search code examples
wpfadornerlayer

Does GetAdornerLayer return the same layer for a control and its adorner?


If I call GetAdornerLayer(control) in a control and then add an adorner to the layer, will calling GetAdornerLayer(adorner) return the same layer? I ask because we have code that adds an adorner in a control, and then need to remove that adorner when the control goes away. You can't call GetAdornerLayer(control) in the control's Unloaded event because it's already been removed from the visual tree at that point, but GetAdornerLayer(adorner) appears to work.


Solution

  • GetAdornerLayer(adorner) returns the AdornerLayer for the adorner, The AdornerLayer is the parent of the Adorner (not the element it adorns).

    Also, it is the same AdornerLayer during the visual tree lifetime, you can cache a reference to it.

    All this can be verified in Reflector.