dotnet/wpf · error · ArgumentException
SR.Invalid_IInputElement
Error message
SR.Invalid_IInputElement
What it means
The static UIElement.AddHandler helper only accepts targets that are UIElement, UIElement3D, or ContentElement. When the given DependencyObject d is none of these, ArgumentException(SR.Invalid_IInputElement) is thrown with the actual type name.
Solutions
- Pass a UIElement, UIElement3D, or ContentElement as the target
- Check the target type before calling and route to the correct AddHandler overload
- Store handlers on a containing element if the object cannot own routed-event handlers
Example fix
// before
UIElement.AddHandler(ev, (DependencyObject)brush, handler); // throws
// after
if (d is UIElement ue) ue.AddHandler(ev, handler);
else if (d is ContentElement ce) ce.AddHandler(ev, handler);
else throw new NotSupportedException($"{d.GetType()} cannot own routed event handlers"); Defensive patterns
Strategy: validation
Validate before calling
if (!(d is UIElement || d is UIElement3D || d is ContentElement))
throw new ArgumentException($"{d.GetType()} cannot own routed-event handlers"); Type guard
static bool CanAddHandlers(DependencyObject d) => d is UIElement || d is UIElement3D || d is ContentElement;
Try / catch
try { UIElement.AddHandler(ev, d, handler); }
catch (ArgumentException ex) when (ex.Message.Contains("IInputElement")) { log.Warn($"Cannot attach handler to {d?.GetType()}", ex); } Prevention
- Type event-target parameters as UIElement/ContentElement, not DependencyObject
- Attach handlers only on elements, not on Freezables or plain DependencyObjects
- Write unit tests for helper wiring code with a non-element DependencyObject
When it happens
Trigger: Calling UIElement.AddHandler(routedEvent, d, handler) with a DependencyObject that is not an event-capable element (e.g. a raw DependencyObject, Freezable, or other non-IInputElement).
Common situations: Generic helper code written against DependencyObject instead of UIElement/ContentElement; wiring handlers on non-element objects like brushes or animations; refactors that widened parameter types.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.Mismatched_RoutedEvent
- SR.HandlerTypeIllegal
- SR.IncorrectLocatorPartType
- SR.MustBeTriggerAction
- SR.SourceNotSet
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/0d9a657678bb39a8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs:1901
uiElement.AddHandler(routedEvent, handler);
}
else
{
ContentElement contentElement = d as ContentElement;
if (contentElement != null)
{
contentElement.AddHandler(routedEvent, handler);
}
else
{
UIElement3D uiElement3D = d as UIElement3D;
if (uiElement3D != null)
{
uiElement3D.AddHandler(routedEvent, handler);
}
else
{
throw new ArgumentException(SR.Format(SR.Invalid_IInputElement, d.GetType()));
}
}
}
}
/// <summary>
/// Removes a handler for the given attached event
/// </summary>
internal static void RemoveHandler(DependencyObject d, RoutedEvent routedEvent, Delegate handler)
{
ArgumentNullException.ThrowIfNull(d);
Debug.Assert(routedEvent != null, "RoutedEvent must not be null");
UIElement uiElement = d as UIElement;
if (uiElement != null)
{
uiElement.RemoveHandler(routedEvent, handler);View on GitHub (pinned to 81131a70a4)