dotnet/wpf · error · ArgumentException
SR.ChildNameMustBeNonEmpty
Error message
SR.ChildNameMustBeNonEmpty
What it means
TriggerBase.ProcessParametersVisualTreeChild throws ArgumentException with SR.ChildNameMustBeNonEmpty when the TargetName given to a trigger setter/action is an empty string. TargetName must reference a named element (via x:Name / Name) within the template; an empty string is meaningless and rejected after the null check.
Solutions
- Provide a valid TargetName matching an x:Name in the template, or omit TargetName to target the templated control
- Validate the name string is non-empty before calling the API
- Fix the source of the empty string (binding, config, concatenation)
Example fix
// before
setter.TargetName = string.Empty;
// after
if (!string.IsNullOrEmpty(name)) { setter.TargetName = name; }
else { /* omit TargetName or fix name source */ } Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(targetName))
throw new ArgumentException("TargetName must be non-empty."); Type guard
static bool IsValidTargetName(string name) => !string.IsNullOrEmpty(name);
Try / catch
try { setter.TargetName = name; }
catch (ArgumentException ex) when (ex.Message.Contains("non-empty") || ex.Message.Contains("empty")) { /* fix name */ } Prevention
- Ensure referenced template elements have x:Name
- Never compute TargetName from possibly-empty strings
- Validate name sources (config, bindings) at startup
When it happens
Trigger: Setting Setter.TargetName="" or TriggerAction/TargetName to an empty string in XAML or via code (SetXxx(dp, "")), e.g. <Setter Property="Background" TargetName="" Value="Red"/>.
Common situations: Data-bound or string-concatenated TargetName that evaluated to empty; typos leaving TargetName=""; dynamic target names computed from config.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- SR.Format(SR.NameNotFound, propertyValue.ChildName)
- Animation_ChildMustBeKeyFrame
- Animation_ChildMustBeKeyFrame
- Animation_NoTextChildren
- Animation_NoTextChildren
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b9c9b863b3244f6c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TriggerBase.cs:191
{
// Not allowed to use Style to affect the StyleProperty.
if (dp == FrameworkElement.StyleProperty)
{
throw new ArgumentException(SR.StylePropertyInStyleNotAllowed);
}
}
/// <summary>
/// Parameter validation work common to the SetXXXX methods that deal
/// with visual tree child nodes.
/// </summary>
internal string ProcessParametersVisualTreeChild(DependencyProperty dp, string target)
{
ArgumentNullException.ThrowIfNull(target);
if (target.Length == 0)
{
throw new ArgumentException(SR.ChildNameMustBeNonEmpty);
}
return String.Intern(target);
}
/// <summary>
/// After the parameters have been validated, store it in the
/// PropertyValues collection.
/// </summary>
/// <remarks>
/// All these will be looked at again (and processed into runtime
/// data structures) by Style.Seal(). We keep them around even after
/// that point should we need to serialize this data back out.
/// </remarks>
internal void AddToPropertyValues(string childName, DependencyProperty dp, object value, PropertyValueType valueType)
{
// Store original data
PropertyValue propertyValue = new PropertyValueView on GitHub (pinned to 81131a70a4)