dotnet/wpf · error · ArgumentException

SR.StringEmpty

Error message

SR.StringEmpty

What it means

DependencyProperty.Register (and its attached/read-only variants) validates its 'name' parameter before creating a property. Throwing when the name is an empty string. WPF requires every dependency property to have a non-empty, owner-unique name used for lookup, XAML binding, and property-system bookkeeping.

Solutions

  1. Pass a non-empty string literal (by convention the CLR property name) as the name argument.
  2. If the name is computed, assert/validate it is non-empty before calling Register.
  3. Prefer nameof(MyProperty) so the compiler guarantees a valid, synced name.

Example fix

// before
public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register("", typeof(int), typeof(MyControl));
// after
public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register(nameof(Value), typeof(int), typeof(MyControl));
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(name)) throw new ArgumentException("Dependency property name must be non-empty", nameof(name));

Type guard

static bool IsValidDpName(string name) => !string.IsNullOrEmpty(name);

Prevention

When it happens

Trigger: Calling DependencyProperty.Register("", typeof(int), typeof(MyControl)) or the same empty-string name via RegisterAttached, RegisterReadOnly, or RegisterAttachedReadOnly.

Common situations: Names built dynamically from variables or resource strings that resolve to "" (e.g. nameof() replaced by a config value that failed to load, or string concatenation where a prefix variable was empty).

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


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs:242

        /// <param name="propertyType">Type of the property</param>
        /// <param name="ownerType">Type that is registering the property</param>
        /// <param name="defaultMetadata">Metadata to use if current type doesn't specify type-specific metadata</param>
        /// <param name="validateValueCallback">Provides additional value validation outside automatic type validation</param>
        /// <returns>Dependency Property</returns>
        public static DependencyProperty RegisterAttached(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)
        {
            RegisterParameterValidation(name, propertyType, ownerType);

            return RegisterCommon( name, propertyType, ownerType, defaultMetadata, validateValueCallback );
        }

        private static void RegisterParameterValidation(string name, Type propertyType, Type ownerType)
        {
            ArgumentNullException.ThrowIfNull(name);

            if (name.Length == 0)
            {
                throw new ArgumentException(SR.StringEmpty, nameof(name));
            }

            ArgumentNullException.ThrowIfNull(ownerType);
            ArgumentNullException.ThrowIfNull(propertyType);
        }

        private static DependencyProperty RegisterCommon(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)
        {
            FromNameKey key = new(name, ownerType);
            lock (Synchronized)
            {
                if (PropertyFromName.ContainsKey(key))
                {
                    throw new ArgumentException(SR.Format(SR.PropertyAlreadyRegistered, name, ownerType.Name));
                }
            }

            // Establish default metadata for all types, if none is provided

View on GitHub (pinned to 81131a70a4)