{"record":{"id":"bfa417c92926a384","repo":"dotnet/wpf","slug":"elementnotenabledexception-windowslistviewitemcheckbox","errorCode":null,"errorMessage":"ElementNotEnabledException","messagePattern":"ElementNotEnabledException","errorType":"exception","errorClass":"System.Windows.Automation.ElementNotEnabledException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewItemCheckBox.cs","lineNumber":241,"sourceCode":"                        return ToggleState.On;\n                    }\n\n                case ListViewItem.CheckState.Unchecked :\n                    {\n                        return ToggleState.Off;\n                    }\n            }\n\n            // developer defined custom values which cannot be interpret outside of the app's scope\n            return ToggleState.Indeterminate;\n        }\n        \n        private void Toggle()\n        {\n            // Make sure that the control is enabled\n            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))\n            {\n                throw new ElementNotEnabledException();\n            }\n\n            Misc.SetFocus(_hwnd);\n\n            NativeMethods.Win32Rect rc = ListViewCheckBoxRect(_hwnd, _listviewItem);\n            NativeMethods.Win32Point pt = new NativeMethods.Win32Point((rc.left + rc.right) / 2, (rc.top + rc.bottom) / 2);\n\n            if (Misc.MapWindowPoints(IntPtr.Zero, _hwnd, ref pt, 1))\n            {\n                // \"click\" on the checkbox\n                Misc.ProxySendMessage(_hwnd, NativeMethods.WM_LBUTTONDOWN, (IntPtr)NativeMethods.MK_LBUTTON, NativeMethods.Util.MAKELPARAM(pt.x, pt.y));\n                Misc.ProxySendMessage(_hwnd, NativeMethods.WM_LBUTTONUP, IntPtr.Zero, NativeMethods.Util.MAKELPARAM(pt.x, pt.y));\n            }\n        }\n\n        #endregion Private Methods\n        \n        //------------------------------------------------------","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsListViewItemCheckBox.cs#L223-L259","documentation":"The UIA Toggle provider for a ListView item checkbox throws ElementNotEnabledException when the checkbox HWND is disabled, per the UIA TogglePattern contract that Toggle is not permitted on disabled elements. The provider checks SafeNativeMethods.IsWindowEnabled(_hwnd) before simulating a click at the checkbox center. It reflects the Win32 state of the underlying list-view item, not a .NET object state.","triggerScenarios":"Calling TogglePattern.Toggle() on a list-view item checkbox whose owning HWND has WS_DISABLED set (IsWindowEnabled returns false). This happens when the ListView control or the item is programmatically disabled, or when a parent window/dialog is disabled (e.g. modal state), since IsWindowEnabled accounts for parent disabling.","commonSituations":"UI automation scripts or tests driving a checkbox inside a list view while the dialog is disabled during a long operation or modal wait; app code disabling the ListView before the automation client clicks; disabled parent window making the child report disabled even though the item itself is fine.","solutions":["Check the element's IsEnabled property (or IsWindowEnabled on the HWND) before calling Toggle and skip/retry when false","Wait for the app to re-enable the control (poll for enabled state or wait for the modal/busy state to end) before toggling","Fix the application so it does not leave the ListView disabled after the operation completes","Catch ElementNotEnabledException in the automation client and treat it as 'control currently unavailable'"],"exampleFix":"// before\n((TogglePattern)checkBox.GetCurrentPattern(TogglePattern.Pattern)).Toggle();\n\n// after\nif (checkBox.Current.IsEnabled)\n{\n    ((TogglePattern)checkBox.GetCurrentPattern(TogglePattern.Pattern)).Toggle();\n}\nelse\n{\n    // wait/retry until the checkbox is enabled\n}","handlingStrategy":"validation","validationCode":"// C#: check before toggling\nAutomationElement checkBox = /* ... */;\nif (!checkBox.Current.IsEnabled)\n    throw new SkipRetryException(\"Checkbox is disabled; wait for the app to enable it\");\n((TogglePattern)checkBox.GetCurrentPattern(TogglePattern.Pattern)).Toggle();","typeGuard":"bool IsTogglable(AutomationElement el) =>\n    el.Current.IsEnabled && el.TryGetCurrentPattern(TogglePattern.Pattern, out _);","tryCatchPattern":"try\n{\n    ((TogglePattern)el.GetCurrentPattern(TogglePattern.Pattern)).Toggle();\n}\ncatch (ElementNotEnabledException)\n{\n    // element disabled; queue/retry after the UI is re-enabled\n}","preventionTips":["Always read the IsEnabled UIA property before invoking patterns","In UI tests, wait for window enabled-state instead of fixed sleeps","Remember IsWindowEnabled returns false if any ancestor window is disabled","Handle the disabled state explicitly rather than treating it as a test failure"],"tags":["uiautomation","togglepattern","disabled-control","win32"],"backgroundTag":"invalid-state-transition","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"}