{"record":{"id":"e861c447d7533bb7","repo":"dotnet/wpf","slug":"elementnotenabledexception-buttonautomationpeer","errorCode":null,"errorMessage":"ElementNotEnabledException","messagePattern":"ElementNotEnabledException","errorType":"exception","errorClass":"ElementNotEnabledException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ButtonAutomationPeer.cs","lineNumber":41,"sourceCode":"        ///\n        protected override AutomationControlType GetAutomationControlTypeCore()\n        {\n            return AutomationControlType.Button;\n        }\n\n        /// \n        public override object GetPattern(PatternInterface patternInterface)\n        {\n            if (patternInterface == PatternInterface.Invoke)\n                return this;\n            else\n                return base.GetPattern(patternInterface);\n        }\n\n        void IInvokeProvider.Invoke()\n        {\n            if(!IsEnabled())\n                throw new ElementNotEnabledException();\n\n            // Async call of click event\n            // In ClickHandler opens a dialog and suspend the execution we don't want to block this thread\n            Dispatcher.BeginInvoke(DispatcherPriority.Input, new DispatcherOperationCallback(delegate(object param)\n            {\n                ((Button)Owner).AutomationButtonBaseClick();\n                return null;\n            }), null);\n        }\n    }\n}\n\n","sourceCodeStart":23,"sourceCodeEnd":54,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/ButtonAutomationPeer.cs#L23-L54","documentation":"ButtonAutomationPeer implements IInvokeProvider.Invoke, which UI Automation clients call to programmatically 'click' a button. If the button's IsEnabled() returns false, the peer throws ElementNotEnabledException, as required by the UIA provider contract, signaling that the Invoke pattern action cannot be performed on a disabled element.","triggerScenarios":"A UI Automation client (test framework, screen reader, automation script) retrieves the InvokePattern of a Button whose IsEnabled is false and calls Invoke() — typically clicking a button disabled because form data is invalid or an async operation is in progress.","commonSituations":"Coded UI / UIA test suites racing against buttons disabled during loading; automation scripts running before data binding enables the button; accessibility clients invoking controls on a disabled form.","solutions":["Wait for the button to become enabled: subscribe to AutomationPropertyChangedEvent for IsEnabledProperty == true or poll before invoking.","Use a retry/wait helper in the test framework instead of invoking immediately.","Fix test timing so the UI state that enables the button is reached first (e.g. fill required fields via ValuePattern).","Wrap Invoke() in try/catch for ElementNotEnabledException and retry after a delay if the disable is transient."],"exampleFix":"// before\nvar pattern = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);\npattern.Invoke(); // throws if disabled\n// after\nif ((bool)button.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty))\n{\n    ((InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern)).Invoke();\n}","handlingStrategy":"validation","validationCode":"bool isEnabled = (bool)button.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty);\nif (!isEnabled) throw new InvalidOperationException(\"Button is disabled; do not Invoke.\");","typeGuard":"bool CanInvoke(AutomationElement el) =>\n    (bool)el.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) &&\n    el.GetSupportedPatterns().Any(p => p.Pattern == InvokePattern.Pattern);","tryCatchPattern":"try { ((InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern)).Invoke(); }\ncatch (ElementNotEnabledException)\n{\n    // wait for IsEnabled == true, then retry\n}","preventionTips":["Wait for IsEnabledProperty change notifications before invoking.","Do not invoke buttons disabled by validation or in-flight async work.","Use polling/retry helpers in UI automation tests."],"tags":["wpf","ui-automation","automation-peer","disabled-control"],"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"}