dotnet/wpf · error · ArgumentException

SR.Format(SR.PropertyAlreadyRegistered, name…

Error message

SR.Format(SR.PropertyAlreadyRegistered, name, ownerType.Name)

What it means

Dependency property names must be unique per owner type. RegisterCommon looks up a FromNameKey(name, ownerType) in the global PropertyFromName table and throws if a property with that name is already registered for that owner type. Duplicate registration usually indicates the static-registration code path is running twice or two properties collide on name.

Solutions

  1. Ensure each property is registered exactly once, typically in the static constructor of its owner class.
  2. Change the name string so it is unique for the given ownerType.
  3. Fix the ownerType argument (use the class that actually declares the property, not a derived/shared type).
  4. If a derived class needs a differently-behaving property, register a new name rather than re-registering the base one.

Example fix

// before (in both BaseControl and DerivedControl static ctors)
public static readonly DependencyProperty FlagProperty =
    DependencyProperty.Register("Flag", typeof(bool), typeof(BaseControl));
// after (DerivedControl declares its own owner)
public static readonly DependencyProperty FlagProperty =
    DependencyProperty.Register("Flag", typeof(bool), typeof(DerivedControl));
Defensive patterns

Strategy: validation

Validate before calling

if (DependencyProperty.FromName(name, ownerType) != null) throw new InvalidOperationException($"{ownerType.Name}.{name} already registered");

Try / catch

try { /* Register */ } catch (ArgumentException ex) when (ex.Message.Contains("already registered")) { /* log duplicate registration bug; do not re-register */ }

Prevention

When it happens

Trigger: Calling Register/RegisterAttached twice with the same (name, ownerType) pair — e.g. in two static ctors, in a base and derived class both declaring ownerType explicitly, or a static initializer re-run via reflection.

Common situations: Copy-pasting a DependencyProperty declaration into another class but forgetting to change the owner type argument; accidental double static initialization from a generic type instantiated multiple times; assembly reload scenarios.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


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

Appendix: source

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

            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
            if (defaultMetadata == null)
            {
                defaultMetadata = AutoGeneratePropertyMetadata( propertyType, validateValueCallback, name, ownerType );
            }
            else // Metadata object is provided.
            {
                // If the defaultValue wasn't specified auto generate one
                if (!defaultMetadata.DefaultValueWasSet())
                {
                    defaultMetadata.DefaultValue = AutoGenerateDefaultValue(propertyType);
                }

                ValidateMetadataDefaultValue( defaultMetadata, propertyType, name, validateValueCallback );
            }

View on GitHub (pinned to 81131a70a4)