{"record":{"id":"5a5d5d551548e4ca","repo":"dotnet/wpf","slug":"sr-dragmovefail","errorCode":null,"errorMessage":"SR.DragMoveFail","messagePattern":"SR\\.DragMoveFail","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":257,"sourceCode":"                return;\n            }\n\n            // Mouse.LeftButton actually reflects the primary button user is using.\n            // So we don't need to check whether the button has been swapped here.\n            if (Mouse.LeftButton == MouseButtonState.Pressed)\n            {\n                if (WindowState == WindowState.Normal)\n                {\n                    // SendMessage's return value is dependent on the message send.  WM_SYSCOMMAND\n                    // and WM_LBUTTONUP return value just signify whether the WndProc handled the\n                    // message or not, so they are not interesting\n                    UnsafeNativeMethods.SendMessage( Handle, WindowMessage.WM_SYSCOMMAND, (IntPtr)NativeMethods.SC_MOUSEMOVE, IntPtr.Zero);\n                    UnsafeNativeMethods.SendMessage( Handle, WindowMessage.WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);\n                }\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.DragMoveFail);\n            }\n}\n\n        /// <summary>\n        ///     Shows the window as a modal window\n        /// </summary>\n        /// <returns>bool?</returns>\n        /// <remarks>\n        ///     Callers must have UIPermission(UIPermissionWindow.AllWindows) to call this API.\n        /// </remarks>\n        public Nullable<bool> ShowDialog()\n        {\n            // this call ends up throwing an exception if ShowDialog\n            // is not allowed\n            VerifyApiSupported();\n            VerifyContextAndObjectState();\n            VerifyCanShow();\n            VerifyNotClosing();","sourceCodeStart":239,"sourceCodeEnd":275,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L239-L275","documentation":"Window.DragMove initiates a mouse-drag window move by sending WM_SYSCOMMAND/SC_MOUSEMOVE, which is only valid while the left mouse button is down. If DragMove is called when the mouse's left button is not pressed (or outside a valid drag context), WPF throws this InvalidOperationException (SR.DragMoveFail).","triggerScenarios":"Calling window.DragMove() outside of a MouseLeftButtonDown handler (e.g. from a Click event, a timer, or before the left button is pressed), or calling it after the mouse-up already occurred.","commonSituations":"Implementing custom chrome/borderless windows and wiring DragMove to MouseLeftButtonUp or a button click instead of MouseLeftButtonDown; calling DragMove asynchronously (Dispatcher.BeginInvoke) after the button state changed; simulating drag on touch without mouse capture.","solutions":["Call DragMove only inside a MouseLeftButtonDown event handler, synchronously","Do not defer the call with Dispatcher.BeginInvoke or async continuations","Verify Mouse.LeftButton == MouseButtonState.Pressed before calling DragMove","For button-click initiated drags, capture mouse on down and call DragMove on the down event instead"],"exampleFix":"// before\nprivate void TitleBar_Click(object sender, RoutedEventArgs e) {\n    DragMove(); // throws: left button not down\n}\n// after\nprivate void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {\n    if (Mouse.LeftButton == MouseButtonState.Pressed) {\n        DragMove();\n    }\n}","handlingStrategy":"validation","validationCode":"if (e is MouseButtonEventArgs mbe && mbe.ChangedButton == MouseButton.Left && Mouse.LeftButton == MouseButtonState.Pressed) { DragMove(); }","typeGuard":"bool CanDragMove() => Mouse.LeftButton == MouseButtonState.Pressed;","tryCatchPattern":"try { DragMove(); } catch (InvalidOperationException ex) when (ex.Message == SR.DragMoveFail) { /* ignore: not in drag state */ }","preventionTips":["Wire DragMove to MouseLeftButtonDown only","Never defer DragMove via Dispatcher or async","Check Mouse.LeftButton state before the call"],"tags":["wpf","window","dragmove","mouse"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}