{"record":{"id":"c24e9776a95107bc","repo":"dotnet/wpf","slug":"new-elementnotenabledexception-valuepattern","errorCode":null,"errorMessage":"new ElementNotEnabledException()","messagePattern":"new ElementNotEnabledException\\(\\)","errorType":"exception","errorClass":"ElementNotEnabledException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/ValuePattern.cs","lineNumber":78,"sourceCode":"        //  Public Methods\n        //\n        //------------------------------------------------------\n \n        #region Public Methods\n\n        /// <summary>\n        /// Request to set the value that this UI element is representing\n        /// </summary>\n        /// <param name=\"value\">Value to set the UI to, the provider is responsible for converting from a string into the appropriate data type</param>\n        public void SetValue( string value )\n        {\n            ArgumentNullException.ThrowIfNull(value);\n\n            // Test the Enabled state prior to the more general Read-Only state.            \n            object enabled = _el.GetCurrentPropertyValue(AutomationElementIdentifiers.IsEnabledProperty);\n            if (enabled is bool && !(bool)enabled)\n            {\n                throw new ElementNotEnabledException();\n            }\n\n            // Test the Read-Only state after the more specific Enabled state.\n            object readOnly = _el.GetCurrentPropertyValue(IsReadOnlyProperty);\n            if (readOnly is bool && (bool)readOnly)\n            {\n                throw new InvalidOperationException(SR.ValueReadonly);\n            }\n\n            UiaCoreApi.ValuePattern_SetValue(_hPattern,  value);\n        }\n\n        #endregion Public Methods\n\n\n        //------------------------------------------------------\n        //\n        //  Public Properties","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/ValuePattern.cs#L60-L96","documentation":"ValuePattern.SetValue throws ElementNotEnabledException when the automation element reports IsEnabled == false. The pattern checks the Enabled state before the read-only state because a disabled control cannot accept a value regardless of writability.","triggerScenarios":"Calling valuePattern.SetValue(\"x\") on a disabled control: a grayed-out/disabled TextBox or ComboBox, a form whose fields are disabled until another option is selected, or an element whose underlying Win32 control is disabled.","commonSituations":"UI test automation running before the UI finishes enabling fields; dialogs disabled while a background operation runs; controls disabled by licensing/permission state.","solutions":["Wait until (bool)element.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) is true before calling SetValue.","Enable the control in the application under test (fix the disabling precondition) rather than bypassing UIA.","Catch ElementNotEnabledException and retry with a timeout/poll loop."],"exampleFix":"// before\nvaluePattern.SetValue(\"hello\"); // control disabled\n// after\nvar enabled = (bool)element.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty);\nif (!enabled) WaitForEnabled(element, TimeSpan.FromSeconds(5));\nvaluePattern.SetValue(\"hello\");","handlingStrategy":"validation","validationCode":"var enabled = element.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty);\nif (enabled is bool isOn && !isOn)\n    throw new SkipException(\"Element is disabled; cannot SetValue.\");","typeGuard":"static bool IsEnabled(AutomationElement el) =>\n    el.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) is bool b && b;","tryCatchPattern":"try { valuePattern.SetValue(text); }\ncatch (ElementNotEnabledException)\n{\n    WaitForCondition(() => IsEnabled(element), TimeSpan.FromSeconds(5));\n    valuePattern.SetValue(text);\n}","preventionTips":["Poll IsEnabled with a timeout before interacting with form fields","In test suites, wait for UI readiness (background work done) before asserting values","Treat ElementNotEnabledException as retryable with backoff, not a test failure"],"tags":["uiautomation","valuepattern","element-not-enabled","disabled-control"],"backgroundTag":"operation-not-supported","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"}