dotnet/wpf · error · ArgumentOutOfRangeException
ArgumentOutOfRangeException(routedEvent)
Error message
ArgumentOutOfRangeException(routedEvent)
What it means
DataObjectEventArgs' internal constructor only accepts the routed events that make sense for data-object operations: DataObject.CopyingEvent, DataObject.PastingEvent, and DataObject.SettingDataEvent. Passing any other RoutedEvent throws ArgumentOutOfRangeException("routedEvent").
Solutions
- Only raise DataObject.CopyingEvent, DataObject.PastingEvent, or DataObject.SettingDataEvent with DataObjectEventArgs
- Derive from RoutedEventArgs instead of DataObjectEventArgs for custom events
- Verify the RoutedEvent static field passed to RaiseEvent belongs to DataObject
Example fix
// before RaiseEvent(new DataObjectEventArgs(MyCustomEvent, isDragDrop: false)); // after RaiseEvent(new RoutedEventArgs(MyCustomEvent)); // custom events use RoutedEventArgs // or: RaiseEvent(new DataObjectEventArgs(DataObject.CopyingEvent, false));
Defensive patterns
Strategy: validation
Validate before calling
bool allowed = routedEvent == DataObject.CopyingEvent || routedEvent == DataObject.PastingEvent || routedEvent == DataObject.SettingDataEvent;
Type guard
static bool IsDataObjectEvent(RoutedEvent e) =>
e == System.Windows.DataObject.CopyingEvent ||
e == System.Windows.DataObject.PastingEvent ||
e == System.Windows.DataObject.SettingDataEvent; Try / catch
try
{
RaiseEvent(new DataObjectEventArgs(routedEvent, false));
}
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "routedEvent")
{
// wrong routed event; use RoutedEventArgs for custom events
} Prevention
- Only use DataObjectEventArgs with the three DataObject.* routed events
- Derive from RoutedEventArgs for custom clipboard-like events
- Reference the exact static RoutedEvent fields from DataObject
When it happens
Trigger: Raising or subclassing DataObjectEventArgs with a foreign RoutedEvent, e.g. raising the event with EventManager-defined or custom routed event IDs, or wiring a DataObjectEventArgs-derived class to a custom routed event.
Common situations: Custom copy/paste pipelines reusing DataObjectEventArgs for their own routed events; reflection-based event raising that passes the wrong RoutedEvent field.
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
- args
- args
- ArgumentOutOfRangeException(authenticationType)
- ArgumentOutOfRangeException
- ArgumentOutOfRangeException(nameof(characterHit))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/7ede984a77163c32.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DataObjectEventArgs.cs:47
/// <summary>
/// Creates a DataObjectCopyEvent.
/// This object created by editors executing a Copy/Paste
/// and Drag/Drop comands.
/// </summary>
/// <param name="routedEvent">
/// An event id. One of: CopyingEvent or PastingEvent
/// </param>
/// <param name="isDragDrop">
/// A flag indicating if this operation is part of drag/drop.
/// Copying event fired on drag start, Pasting - on drop.
/// Cancelling the command stops drag/drop process in
/// an appropriate moment.
/// </param>
internal DataObjectEventArgs(RoutedEvent routedEvent, bool isDragDrop) : base()
{
if (routedEvent != DataObject.CopyingEvent && routedEvent != DataObject.PastingEvent && routedEvent != DataObject.SettingDataEvent)
{
throw new ArgumentOutOfRangeException(nameof(routedEvent));
}
RoutedEvent = routedEvent;
_isDragDrop = isDragDrop;
_commandCancelled = false;
}
#endregion Constructors
//------------------------------------------------------
//
// Public Properties
//
//------------------------------------------------------
#region Public Properties
View on GitHub (pinned to 81131a70a4)