dotnet/wpf · error · ArgumentNullException

control

Error message

control

What it means

CalendarVisualStates.GoToState is a helper that forwards a control to VisualStateManager.GoToState for the first matching state name. It validates that the Control argument is non-null and throws ArgumentNullException named "control" otherwise.

Solutions

  1. Pass a valid, non-null Control instance.
  2. Check template part lookups: if control is null because a part wasn't found, fix the template/part name instead.
  3. If you own the call site, guard with a null check or use ArgumentNullException.ThrowIfNull semantics before calling.

Example fix

// before
CalendarVisualStates.GoToState(part, true, "Normal");
// after
if (part != null)
    CalendarVisualStates.GoToState(part, true, "Normal");
Defensive patterns

Strategy: type-guard

Validate before calling

if (control == null) return; // or log missing template part

Type guard

static bool CanGoToState(Control c) => c != null;

Try / catch

try { CalendarVisualStates.GoToState(control, useTransitions, states); }
catch (ArgumentNullException ex) { log.Error("GoToState called with null control", ex); }

Prevention

When it happens

Trigger: Calling CalendarVisualStates.GoToState(null, true, "StateName") — passing a null control, often from a templated-control code path where the templated parent or a part lookup returned null.

Common situations: Custom control OnApplyTemplate where GetTemplateChild returned null and the result is passed to GoToState; generic code invoking the helper before the control instance is created.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/a78936c2881005db. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/CalendarVisualStates.cs:148

        /// <summary>
        /// Use VisualStateManager to change the visual state of the control.
        /// </summary>
        /// <param name="control">
        /// Control whose visual state is being changed.
        /// </param>
        /// <param name="useTransitions">
        /// true to use transitions when updating the visual state, false to
        /// snap directly to the new visual state.
        /// </param>
        /// <param name="stateNames">
        /// Ordered list of state names and fallback states to transition into.
        /// Only the first state to be found will be used.
        /// </param>
        public static void GoToState(Control control, bool useTransitions, params string[] stateNames)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }

            if (stateNames == null)
            {
                return;
            }

            foreach (string name in stateNames)
            {
                if (VisualStateManager.GoToState(control, name, useTransitions))
                {
                    break;
                }
            }
        }
    }
}

View on GitHub (pinned to 81131a70a4)