dotnet/wpf · error · ArgumentException

SR.ScopeMustBeUIElementOrContent

Error message

SR.ScopeMustBeUIElementOrContent

What it means

DragDrop.DoDragDrop throws ArgumentException when the dragSource argument is neither a UIElement, ContentElement, nor UIElement3D. WPF drag-and-drop can only be initiated from these element types, since the source must be able to raise the drag-related routed events.

Solutions

  1. Pass a UIElement, ContentElement, or UIElement3D as dragSource
  2. If the drag origin is a DrawingVisual, route the drag from its containing UIElement host
  3. Find the nearest UIElement ancestor of the intended source and use it

Example fix

// before
DragDrop.DoDragDrop(visual, data, DragDropEffects.Move);
// after
var host = visual as UIElement ?? FindAncestor<UIElement>(visual);
if (host != null)
{
    DragDrop.DoDragDrop(host, data, DragDropEffects.Move);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(dragSource is UIElement || dragSource is ContentElement || dragSource is UIElement3D))
    throw new ArgumentException("dragSource must be UIElement, ContentElement, or UIElement3D.", nameof(dragSource));

Type guard

bool IsValidDragSource(object o) => o is UIElement || o is ContentElement || o is UIElement3D;

Try / catch

try { DragDrop.DoDragDrop(dragSource, data, DragDropEffects.Move); }
catch (ArgumentException) { /* find element host and retry */ }

Prevention

When it happens

Trigger: Calling DragDrop.DoDragDrop(dragSource, data, allowedEffects) with dragSource being a plain DependencyObject, a Visual that is not an element (e.g. DrawingVisual), a model/viewmodel object, or a non-WPF FrameworkElement host.

Common situations: Starting a drag from a DrawingVisual or a ViewModel in an MVVM setup; passing a window's child control retrieved via a non-typed API; interop code passing an HWND-backed handle wrapper instead of the element.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DragDrop.cs:393

            RoutedEventArgs args = new RoutedEventArgs(DragDropStartedEvent, dragSource);
            
            // Raise the DragDropStarted internal event(Bubble).
            if (dragSource is UIElement)
            {
                ((UIElement)dragSource).RaiseEvent(args);
            }
            else if (dragSource is ContentElement)
            {
                ((ContentElement)dragSource).RaiseEvent(args);
            }
            else if (dragSource is UIElement3D)
            {
                ((UIElement3D)dragSource).RaiseEvent(args);
            }
            else
            {
                throw new ArgumentException(SR.ScopeMustBeUIElementOrContent, nameof(dragSource));
            }

            dataObject = data as DataObject;

            if (dataObject == null)
            {
                // Create DataObject for DragDrop from the data.
                dataObject = new DataObject(data);
            }

            // Call OleDoDragDrop with DataObject.
            DragDropEffects ret = OleDoDragDrop(dragSource, dataObject, allowedEffects);

            args = new RoutedEventArgs(DragDropCompletedEvent, dragSource);
            
            // Raise the DragDropCompleted internal event(Bubble).
            if (dragSource is UIElement)
            {

View on GitHub (pinned to 81131a70a4)