{"record":{"id":"58ac296dc93c4930","repo":"MahApps/MahApps.Metro","slug":"the-property-isspellcheckcontextmenuenabled-may","errorCode":null,"errorMessage":"The property 'IsSpellCheckContextMenuEnabled' may only be set on TextBoxBase elements.","messagePattern":"The property 'IsSpellCheckContextMenuEnabled' may only be set on TextBoxBase elements\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/MahApps.Metro/Controls/Helper/TextBoxHelper.cs","lineNumber":701,"sourceCode":"        /// </summary>\n        [Category(AppName.MahApps)]\n        [AttachedPropertyBrowsableForType(typeof(TextBoxBase))]\n        public static bool GetIsSpellCheckContextMenuEnabled(UIElement element)\n        {\n            return (bool)element.GetValue(IsSpellCheckContextMenuEnabledProperty);\n        }\n\n        [AttachedPropertyBrowsableForType(typeof(TextBoxBase))]\n        public static void SetIsSpellCheckContextMenuEnabled(UIElement element, bool value)\n        {\n            element.SetValue(IsSpellCheckContextMenuEnabledProperty, BooleanBoxes.Box(value));\n        }\n\n        private static void IsSpellCheckContextMenuEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)\n        {\n            if (!(d is TextBoxBase textBox))\n            {\n                throw new InvalidOperationException(\"The property 'IsSpellCheckContextMenuEnabled' may only be set on TextBoxBase elements.\");\n            }\n\n            if (e.OldValue != e.NewValue)\n            {\n                textBox.SetCurrentValue(SpellCheck.IsEnabledProperty, BooleanBoxes.Box((bool)e.NewValue));\n                if ((bool)e.NewValue)\n                {\n                    textBox.ContextMenuOpening += TextBoxBaseContextMenuOpening;\n                    textBox.LostFocus += TextBoxBaseLostFocus;\n                    textBox.ContextMenuClosing += TextBoxBaseContextMenuClosing;\n                }\n                else\n                {\n                    textBox.ContextMenuOpening -= TextBoxBaseContextMenuOpening;\n                    textBox.LostFocus -= TextBoxBaseLostFocus;\n                    textBox.ContextMenuClosing -= TextBoxBaseContextMenuClosing;\n                }\n            }","sourceCodeStart":683,"sourceCodeEnd":719,"githubUrl":"https://github.com/MahApps/MahApps.Metro/blob/72099e310bac2d12ac98fd7560b69679252519f5/src/MahApps.Metro/Controls/Helper/TextBoxHelper.cs#L683-L719","documentation":"Thrown by IsSpellCheckContextMenuEnabledChanged when the dependency object is not a TextBoxBase (TextBox, RichTextBox, FlowDocumentReader-ish text boxes). The attached property wires SpellCheck and context-menu handlers; setting it on any non-TextBoxBase element is rejected. Though marked AttachedPropertyBrowsableForType(TextBoxBase), the change callback still validates the runtime type.","triggerScenarios":"Setting TextBoxHelper.IsSpellCheckContextMenuEnabled on a non-text element (ComboBox, PasswordBox, TextBlock, Label, or custom control); applying via code-behind SetValue on an arbitrary UIElement.","commonSituations":"Confusing IsSpellCheckContextMenuEnabled with a general property and applying it to a PasswordBox (which derives from Control, not TextBoxBase) or a ComboBox; copy-paste from a TextBox onto an incompatible control.","solutions":["Set IsSpellCheckContextMenuEnabled only on TextBoxBase-derived controls (TextBox, RichTextBox).","For PasswordBox spell-check, note PasswordBox is not a TextBoxBase; use alternative approaches.","Remove the attached property from non-text elements.","Type-check the target before setting the attached property in code."],"exampleFix":"<!-- before -->\n<PasswordBox TextBoxHelper.IsSpellCheckContextMenuEnabled=\"True\" /> <!-- throws -->\n\n<!-- after -->\n<TextBox TextBoxHelper.IsSpellCheckContextMenuEnabled=\"True\" />","handlingStrategy":"type-guard","validationCode":"if (d is not TextBoxBase)\n{\n    throw new ArgumentException(\n        \"IsSpellCheckContextMenuEnabled requires a TextBoxBase-derived element.\");\n}","typeGuard":"static bool IsTextBoxBase(DependencyObject d) => d is TextBoxBase;","tryCatchPattern":null,"preventionTips":["Set IsSpellCheckContextMenuEnabled only on TextBoxBase-derived controls (TextBox, RichTextBox).","PasswordBox is NOT a TextBoxBase; do not use this property on it.","Type-check the target before setting the attached property in code."],"tags":["wpf","textbox-helper","mahapps","spellcheck","type-guard","configuration"],"backgroundTag":null,"analyzedSha":"72099e310bac2d12ac98fd7560b69679252519f5","analyzedAt":"2026-08-13T20:19:34.419Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}