{"record":{"id":"20ea79f4bb4da4c0","repo":"dotnet/wpf","slug":"sr-scopemustbeuielementorcontent","errorCode":null,"errorMessage":"SR.ScopeMustBeUIElementOrContent","messagePattern":"SR\\.ScopeMustBeUIElementOrContent","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DragDrop.cs","lineNumber":393,"sourceCode":"\n            RoutedEventArgs args = new RoutedEventArgs(DragDropStartedEvent, dragSource);\n            \n            // Raise the DragDropStarted internal event(Bubble).\n            if (dragSource is UIElement)\n            {\n                ((UIElement)dragSource).RaiseEvent(args);\n            }\n            else if (dragSource is ContentElement)\n            {\n                ((ContentElement)dragSource).RaiseEvent(args);\n            }\n            else if (dragSource is UIElement3D)\n            {\n                ((UIElement3D)dragSource).RaiseEvent(args);\n            }\n            else\n            {\n                throw new ArgumentException(SR.ScopeMustBeUIElementOrContent, nameof(dragSource));\n            }\n\n            dataObject = data as DataObject;\n\n            if (dataObject == null)\n            {\n                // Create DataObject for DragDrop from the data.\n                dataObject = new DataObject(data);\n            }\n\n            // Call OleDoDragDrop with DataObject.\n            DragDropEffects ret = OleDoDragDrop(dragSource, dataObject, allowedEffects);\n\n            args = new RoutedEventArgs(DragDropCompletedEvent, dragSource);\n            \n            // Raise the DragDropCompleted internal event(Bubble).\n            if (dragSource is UIElement)\n            {","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DragDrop.cs#L375-L411","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a UIElement, ContentElement, or UIElement3D as dragSource","If the drag origin is a DrawingVisual, route the drag from its containing UIElement host","Find the nearest UIElement ancestor of the intended source and use it"],"exampleFix":"// before\nDragDrop.DoDragDrop(visual, data, DragDropEffects.Move);\n// after\nvar host = visual as UIElement ?? FindAncestor<UIElement>(visual);\nif (host != null)\n{\n    DragDrop.DoDragDrop(host, data, DragDropEffects.Move);\n}","handlingStrategy":"type-guard","validationCode":"if (!(dragSource is UIElement || dragSource is ContentElement || dragSource is UIElement3D))\n    throw new ArgumentException(\"dragSource must be UIElement, ContentElement, or UIElement3D.\", nameof(dragSource));","typeGuard":"bool IsValidDragSource(object o) => o is UIElement || o is ContentElement || o is UIElement3D;","tryCatchPattern":"try { DragDrop.DoDragDrop(dragSource, data, DragDropEffects.Move); }\ncatch (ArgumentException) { /* find element host and retry */ }","preventionTips":["Always initiate drags from a UIElement/ContentElement/UIElement3D","In MVVM, pass the associated view element, not the view-model","Resolve DrawingVisuals to their hosting UIElement before dragging"],"tags":["wpf","argumentexception","drag-drop","wrong-type"],"backgroundTag":"incompatible-source-type","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}