{"record":{"id":"38c76a5fd58c21b2","repo":"dotnet/wpf","slug":"sr-format-sr-propertyalreadyregistered-name-ownertype-name","errorCode":null,"errorMessage":"SR.Format(SR.PropertyAlreadyRegistered, name, ownerType.Name)","messagePattern":"SR\\.Format\\(SR\\.PropertyAlreadyRegistered, name, ownerType\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs","lineNumber":256,"sourceCode":"            ArgumentNullException.ThrowIfNull(name);\n\n            if (name.Length == 0)\n            {\n                throw new ArgumentException(SR.StringEmpty, nameof(name));\n            }\n\n            ArgumentNullException.ThrowIfNull(ownerType);\n            ArgumentNullException.ThrowIfNull(propertyType);\n        }\n\n        private static DependencyProperty RegisterCommon(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)\n        {\n            FromNameKey key = new(name, ownerType);\n            lock (Synchronized)\n            {\n                if (PropertyFromName.ContainsKey(key))\n                {\n                    throw new ArgumentException(SR.Format(SR.PropertyAlreadyRegistered, name, ownerType.Name));\n                }\n            }\n\n            // Establish default metadata for all types, if none is provided\n            if (defaultMetadata == null)\n            {\n                defaultMetadata = AutoGeneratePropertyMetadata( propertyType, validateValueCallback, name, ownerType );\n            }\n            else // Metadata object is provided.\n            {\n                // If the defaultValue wasn't specified auto generate one\n                if (!defaultMetadata.DefaultValueWasSet())\n                {\n                    defaultMetadata.DefaultValue = AutoGenerateDefaultValue(propertyType);\n                }\n\n                ValidateMetadataDefaultValue( defaultMetadata, propertyType, name, validateValueCallback );\n            }","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs#L238-L274","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure each property is registered exactly once, typically in the static constructor of its owner class.","Change the name string so it is unique for the given ownerType.","Fix the ownerType argument (use the class that actually declares the property, not a derived/shared type).","If a derived class needs a differently-behaving property, register a new name rather than re-registering the base one."],"exampleFix":"// before (in both BaseControl and DerivedControl static ctors)\npublic static readonly DependencyProperty FlagProperty =\n    DependencyProperty.Register(\"Flag\", typeof(bool), typeof(BaseControl));\n// after (DerivedControl declares its own owner)\npublic static readonly DependencyProperty FlagProperty =\n    DependencyProperty.Register(\"Flag\", typeof(bool), typeof(DerivedControl));","handlingStrategy":"validation","validationCode":"if (DependencyProperty.FromName(name, ownerType) != null) throw new InvalidOperationException($\"{ownerType.Name}.{name} already registered\");","typeGuard":null,"tryCatchPattern":"try { /* Register */ } catch (ArgumentException ex) when (ex.Message.Contains(\"already registered\")) { /* log duplicate registration bug; do not re-register */ }","preventionTips":["Register DPs once in the static constructor of the declaring owner type","Use nameof and typeof(DeclaringClass) consistently; never share ownerType across classes","Avoid registering DPs in generic type static ctors that run per instantiation"],"tags":["wpf","dependency-property","duplicate-registration"],"backgroundTag":"file-already-exists","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}