MahApps/MahApps.Metro · error · InvalidOperationException

You have missed to specify PART_NumericUp, PART_NumericDown

Error message

You have missed to specify PART_NumericUp, PART_NumericDown or PART_TextBox in your template!

What it means

Thrown by NumericUpDown.OnApplyTemplate when any of PART_NumericUp, PART_NumericDown, or PART_TextBox is null after GetTemplateChild. Unlike the MissingRequiredTemplatePartException pattern used elsewhere, this is a plain InvalidOperationException with a combined message. The control requires RepeatButtons for up/down and a TextBox for value display.

Source

Thrown at src/MahApps.Metro/Controls/NumericUpDown.cs:1008

            }
        }

        /// <summary>
        ///     When overridden in a derived class, is invoked whenever application code or internal processes call
        ///     <see cref="M:System.Windows.FrameworkElement.ApplyTemplate" />.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.repeatUp = this.GetTemplateChild(PART_NumericUp) as RepeatButton;
            this.repeatDown = this.GetTemplateChild(PART_NumericDown) as RepeatButton;

            this.valueTextBox = this.GetTemplateChild(PART_TextBox) as TextBox;

            if (this.repeatUp is null || this.repeatDown is null || this.valueTextBox is null)
            {
                throw new InvalidOperationException($"You have missed to specify {PART_NumericUp}, {PART_NumericDown} or {PART_TextBox} in your template!");
            }

            this.ToggleReadOnlyMode(this.IsReadOnly);

            this.repeatUp.Click += (_, _) => { this.ChangeValueWithSpeedUp(true); };
            this.repeatDown.Click += (_, _) => { this.ChangeValueWithSpeedUp(false); };

            this.repeatUp.PreviewMouseUp += (_, _) => this.ResetInternal();
            this.repeatDown.PreviewMouseUp += (_, _) => this.ResetInternal();

            this.OnValueChanged(this.Value, this.Value);

            this.scrollViewer = null;
        }

        /// <summary>
        /// Creates AutomationPeer (<see cref="UIElement.OnCreateAutomationPeer"/>)
        /// </summary>

View on GitHub (pinned to 72099e310b)

Solutions

  1. Merge MahApps.Metro theme resources so the default NumericUpDown template (with all three parts) is applied.
  2. If re-templating, include RepeatButton 'PART_NumericUp', RepeatButton 'PART_NumericDown', and TextBox 'PART_TextBox'.
  3. Verify no other style hides the default template via BasedOn chains.

Example fix

<!-- before -->
<ControlTemplate TargetType="{x:Type controls:NumericUpDown}">
    <TextBox x:Name="PART_TextBox"/>
</ControlTemplate>

<!-- after -->
<ControlTemplate TargetType="{x:Type controls:NumericUpDown}">
    <Grid>
        <TextBox x:Name="PART_TextBox"/>
        <RepeatButton x:Name="PART_NumericUp" Content="+"/>
        <RepeatButton x:Name="PART_NumericDown" Content="-"/>
    </Grid>
</ControlTemplate>
Defensive patterns

Strategy: validation

Validate before calling

// Ensure theme is applied
var up = numeric.Template?.FindName("PART_NumericUp", numeric) as RepeatButton;
var down = numeric.Template?.FindName("PART_NumericDown", numeric) as RepeatButton;
var box = numeric.Template?.FindName("PART_TextBox", numeric) as TextBox;
if (up == null || down == null || box == null)
    throw new InvalidOperationException("NumericUpDown template is missing required parts");

Try / catch

try
{
    numeric.ApplyTemplate();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("PART_NumericUp"))
{
    // load default MahApps theme
}

Prevention

When it happens

Trigger: Custom ControlTemplate for NumericUpDown missing one or more of: a RepeatButton named 'PART_NumericUp', a RepeatButton named 'PART_NumericDown', or a TextBox named 'PART_TextBox'. Also if the default MahApps theme is not applied.

Common situations: Developer creates a custom NumericUpDown template without all three named elements. MahApps resource dictionaries not merged in App.xaml. Theme stripped or overridden by a generic style.

Related errors


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