MahApps/MahApps.Metro · error · InvalidOperationException

The property 'IsSpellCheckContextMenuEnabled' may only be se

Error message

The property 'IsSpellCheckContextMenuEnabled' may only be set on TextBoxBase elements.

What it means

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.

Source

Thrown at src/MahApps.Metro/Controls/Helper/TextBoxHelper.cs:701

        /// </summary>
        [Category(AppName.MahApps)]
        [AttachedPropertyBrowsableForType(typeof(TextBoxBase))]
        public static bool GetIsSpellCheckContextMenuEnabled(UIElement element)
        {
            return (bool)element.GetValue(IsSpellCheckContextMenuEnabledProperty);
        }

        [AttachedPropertyBrowsableForType(typeof(TextBoxBase))]
        public static void SetIsSpellCheckContextMenuEnabled(UIElement element, bool value)
        {
            element.SetValue(IsSpellCheckContextMenuEnabledProperty, BooleanBoxes.Box(value));
        }

        private static void IsSpellCheckContextMenuEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is TextBoxBase textBox))
            {
                throw new InvalidOperationException("The property 'IsSpellCheckContextMenuEnabled' may only be set on TextBoxBase elements.");
            }

            if (e.OldValue != e.NewValue)
            {
                textBox.SetCurrentValue(SpellCheck.IsEnabledProperty, BooleanBoxes.Box((bool)e.NewValue));
                if ((bool)e.NewValue)
                {
                    textBox.ContextMenuOpening += TextBoxBaseContextMenuOpening;
                    textBox.LostFocus += TextBoxBaseLostFocus;
                    textBox.ContextMenuClosing += TextBoxBaseContextMenuClosing;
                }
                else
                {
                    textBox.ContextMenuOpening -= TextBoxBaseContextMenuOpening;
                    textBox.LostFocus -= TextBoxBaseLostFocus;
                    textBox.ContextMenuClosing -= TextBoxBaseContextMenuClosing;
                }
            }

View on GitHub (pinned to 72099e310b)

Solutions

  1. Set IsSpellCheckContextMenuEnabled only on TextBoxBase-derived controls (TextBox, RichTextBox).
  2. For PasswordBox spell-check, note PasswordBox is not a TextBoxBase; use alternative approaches.
  3. Remove the attached property from non-text elements.
  4. Type-check the target before setting the attached property in code.

Example fix

<!-- before -->
<PasswordBox TextBoxHelper.IsSpellCheckContextMenuEnabled="True" /> <!-- throws -->

<!-- after -->
<TextBox TextBoxHelper.IsSpellCheckContextMenuEnabled="True" />
Defensive patterns

Strategy: type-guard

Validate before calling

if (d is not TextBoxBase)
{
    throw new ArgumentException(
        "IsSpellCheckContextMenuEnabled requires a TextBoxBase-derived element.");
}

Type guard

static bool IsTextBoxBase(DependencyObject d) => d is TextBoxBase;

Prevention

When it happens

Trigger: Setting TextBoxHelper.IsSpellCheckContextMenuEnabled on a non-text element (ComboBox, PasswordBox, TextBlock, Label, or custom control); applying via code-behind SetValue on an arbitrary UIElement.

Common situations: 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.

Related errors


AI-assisted analysis of MahApps/MahApps.Metro@72099e310b (2026-08-13). Data as JSON: /api/errors/58ac296dc93c4930. Report an issue: GitHub.