dotnet/wpf · error · ArgumentException

SR.Format(SR.Invalid_IInputElement, e.GetType())

Error message

SR.Format(SR.Invalid_IInputElement, e.GetType())

What it means

PresentationSource.RemoveSourceChangedHandler applies the same validity rule as the Add counterpart: the element must be a UIElement, ContentElement, or UIElement3D. Otherwise an ArgumentException with the element's type is thrown, even when attempting to unsubscribe.

Solutions

  1. Unsubscribe using exactly the same UIElement/ContentElement/UIElement3D instance used at subscription time.
  2. Guard removal with the same type check used before subscribing.
  3. Revert custom element types to standard WPF element classes.
  4. Wrap cleanup in a type check to skip unsupported elements rather than assuming add succeeded.

Example fix

// before
PresentationSource.RemoveSourceChangedHandler(unknownElement, handler);

// after
if (unknownElement is UIElement || unknownElement is ContentElement || unknownElement is UIElement3D)
{
    PresentationSource.RemoveSourceChangedHandler(unknownElement, handler);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (e is UIElement || e is ContentElement || e is UIElement3D)
    PresentationSource.RemoveSourceChangedHandler(e, handler);

Type guard

static bool CanUnsubscribe(IInputElement e) =>
    e is UIElement || e is ContentElement || e is UIElement3D;

Try / catch

try { PresentationSource.RemoveSourceChangedHandler(e, handler); }
catch (ArgumentException ex) when (ex.ParamName == "e") { /* skip unsupported element */ }

Prevention

When it happens

Trigger: Calling RemoveSourceChangedHandler with an unsupported IInputElement implementer or an object of the wrong kind (e.g. a raw DependencyObject that is not an input element).

Common situations: Symmetric cleanup code that removes handlers from the same mocked/unrecognized element it subscribed with; refactoring that changed element base classes from UIElement to a custom class.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs:185

        /// <summary>
        ///     Removes a handler for the SourceChanged event to the element.
        /// </summary>
        /// <param name="e">The element to remove the handler from.</param>
        /// <param name="handler">The hander to remove.</param>
        /// <remarks>
        ///     Even though this is a routed event handler, there are special
        ///     restrictions placed on this event.
        ///     1) You cannot use the UIElement or ContentElement RemoveHandler() method.
        /// </remarks>
        public static void RemoveSourceChangedHandler(IInputElement e, SourceChangedEventHandler handler)
        {
            ArgumentNullException.ThrowIfNull(e);

            // Either UIElement, ContentElement or UIElement3D.
            if (!InputElement.IsValid(e))
            {
                throw new ArgumentException(SR.Format(SR.Invalid_IInputElement, e.GetType()), nameof(e));
            }
            DependencyObject o = (DependencyObject)e;

            //             o.VerifyAccess();

            // I would rather throw an exception here, but the CLR doesn't
            // so we won't either.
            if (handler != null)
            {
                FrugalObjectList<RoutedEventHandlerInfo> info = null;
                EventHandlersStore store;


                // Either UIElement, ContentElement or UIElement3D.
                if (o is UIElement uie)

                {
                    uie.RemoveHandler(SourceChangedEvent, handler);

View on GitHub (pinned to 81131a70a4)