MahApps/MahApps.Metro · error · MissingRequiredTemplatePartException

Template part "{templatePart}" in template for "{target.GetT

Error message

Template part "{templatePart}" in template for "{target.GetType().FullName}" is missing.

What it means

Thrown by RangeSlider.OnApplyTemplate when GetTemplateChild('PART_Container') does not return a FrameworkElement. MissingRequiredTemplatePartException fires because the slider's layout logic depends on this container for measuring and arranging the thumb/track elements.

Source

Thrown at src/MahApps.Metro/Controls/RangeSlider.cs:1315

            this.ReCalculateRangeSelected(true, true, this._direction);
            this.CoerceLowerUpperValues();
        }

        public void ResetSelection(bool isStart)
        {
            var widthChange = this.Maximum - this.Minimum;
            widthChange = isStart ? -widthChange : widthChange;

            MoveThumb(this._leftButton, this._rightButton, widthChange, this.Orientation, out this._direction);
            this.ReCalculateRangeSelected(true, true, this._direction);
            this.CoerceLowerUpperValues();
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this._container = this.GetTemplateChild("PART_Container") as FrameworkElement ?? throw new MissingRequiredTemplatePartException(this, "PART_Container");
            this._visualElementsContainer = this.GetTemplateChild("PART_RangeSliderContainer") as StackPanel ?? throw new MissingRequiredTemplatePartException(this, "PART_RangeSliderContainer");
            this._centerThumb = this.GetTemplateChild("PART_MiddleThumb") as Thumb ?? throw new MissingRequiredTemplatePartException(this, "PART_MiddleThumb");
            this._leftButton = this.GetTemplateChild("PART_LeftEdge") as RepeatButton ?? throw new MissingRequiredTemplatePartException(this, "PART_LeftEdge");
            this._rightButton = this.GetTemplateChild("PART_RightEdge") as RepeatButton ?? throw new MissingRequiredTemplatePartException(this, "PART_RightEdge");
            this._leftThumb = this.GetTemplateChild("PART_LeftThumb") as Thumb ?? throw new MissingRequiredTemplatePartException(this, "PART_LeftThumb");
            this._rightThumb = this.GetTemplateChild("PART_RightThumb") as Thumb ?? throw new MissingRequiredTemplatePartException(this, "PART_RightThumb");

            this.InitializeVisualElementsContainer();
            this.ReCalculateSize();
        }

        //adds visual element to the container
        private void InitializeVisualElementsContainer()
        {
            this._leftThumb.DragCompleted -= this.LeftThumbDragComplete;
            this._rightThumb.DragCompleted -= this.RightThumbDragComplete;
            this._leftThumb.DragStarted -= this.LeftThumbDragStart;
            this._rightThumb.DragStarted -= this.RightThumbDragStart;

View on GitHub (pinned to 72099e310b)

Solutions

  1. Merge MahApps.Metro theme resources so the default RangeSlider template is applied.
  2. If re-templating, include a FrameworkElement named 'PART_Container'.
  3. Verify the control resolves its default style from the MahApps themes.
Defensive patterns

Strategy: validation

Validate before calling

var container = slider.Template?.FindName("PART_Container", slider) as FrameworkElement;
if (container == null) throw new InvalidOperationException("RangeSlider missing PART_Container");

Try / catch

try
{
    slider.ApplyTemplate();
}
catch (MahAppsException ex) when (ex.Message.Contains("PART_Container"))
{
    // load default theme
}

Prevention

When it happens

Trigger: Custom ControlTemplate for RangeSlider missing a FrameworkElement named 'PART_Container'. Default MahApps theme not applied due to missing resource dictionaries.

Common situations: Custom RangeSlider template without the required parts. MahApps theme dictionaries not merged in App.xaml. Generic style hiding the default template.

Related errors


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