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

  1. Only raise DataObject.CopyingEvent, DataObject.PastingEvent, or DataObject.SettingDataEvent with DataObjectEventArgs
  2. Derive from RoutedEventArgs instead of DataObjectEventArgs for custom events
  3. 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

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


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)