dotnet/wpf · error · InvalidOperationException

SR.AdornerNotFound

Error message

SR.AdornerNotFound

What it means

AdornerLayer.SetAdornerZOrder throws InvalidOperationException(SR.AdornerNotFound) when the adorned element IS registered on the layer, but this specific adorner instance has no AdornerInfo there — the adorner itself is not (or no longer) present. The layer found the element's adorner list but no matching entry for this adorner.

Solutions

  1. Confirm via Array.IndexOf(AdornerLayer.GetAdorners(element), adorner) >= 0 that this exact instance is still attached.
  2. Re-add the adorner with layer.Add(adorner) before setting z-order if it was removed.
  3. Track adorner instances in a single owner so removal and z-order updates use the same reference.
  4. Wrap in try/catch for InvalidOperationException in frameworks where adorner lifetime is external.

Example fix

// before
adornerLayer.SetAdornerZOrder(staleAdorner, 1); // AdornerNotFound
// after
var adorners = AdornerLayer.GetAdorners(element);
if (adorners != null && adorners.Contains(adorner))
    adornerLayer.SetAdornerZOrder(adorner, 1);
Defensive patterns

Strategy: validation

Validate before calling

var adorners = AdornerLayer.GetAdorners(adorner.AdornedElement);
if (adorners == null || Array.IndexOf(adorners, adorner) < 0) return; // skip, not attached

Type guard

bool IsAttached(this Adorner a) { var l = AdornerLayer.GetAdorners(a.AdornedElement); return l != null && l.Contains(a); }

Try / catch

try { adornerLayer.SetAdornerZOrder(adorner, z); } catch (InvalidOperationException) { /* instance detached; recreate and re-add */ }

Prevention

When it happens

Trigger: Calling SetAdornerZOrder with an adorner that was removed via Remove/RemoveAllAdorners while other adorners for the same element remain; passing a new/clone adorner instance that was never added; duplicate adorners where one instance was already disposed.

Common situations: Holding two references to what you assume is one adorner but is actually two instances; removing an adorner in one code path while another path later adjusts its z-order; re-creating adorners on data refresh and updating z-order on the stale instance.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/e699ff8e76d5671a. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/AdornerLayer.cs:567

            UpdateAdorner(null);
        }

        /// <summary>
        /// Set the zOrder on the given adorner.
        /// </summary>
        /// <param name="adorner"></param>
        /// <param name="zOrder"></param>
        internal void SetAdornerZOrder(Adorner adorner, int zOrder)
        {
            ArrayList adornerInfos = ElementMap[adorner.AdornedElement] as ArrayList;
            if (adornerInfos == null)
            {
                throw new InvalidOperationException(SR.AdornedElementNotFound);
            }
            AdornerInfo adornerInfo = GetAdornerInfo(adornerInfos, adorner);
            if (adornerInfo == null)
            {
                throw new InvalidOperationException(SR.AdornerNotFound);
            }

            RemoveAdornerInfo(_zOrderMap, adorner, adornerInfo.ZOrder);
            _children.Remove(adorner);
            adornerInfo.ZOrder = zOrder;
            AddAdornerToVisualTree(adornerInfo, zOrder);
            InvalidateAdorner(adornerInfo);
            UpdateAdorner(adorner.AdornedElement);
        }

        /// <summary>
        /// Query the zOrder on the given adorner.
        /// </summary>
        /// <param name="adorner"></param>
        /// <returns>zOrder of given adorner</returns>
        internal int GetAdornerZOrder(Adorner adorner)
        {
            ArrayList adornerInfos = ElementMap[adorner.AdornedElement] as ArrayList;

View on GitHub (pinned to 81131a70a4)