{"record":{"id":"05bf49073dea7530","repo":"dotnet/wpf","slug":"a-property-mapping-with-the-same-name-already-exists","errorCode":null,"errorMessage":"A property mapping with the same name already exists.","messagePattern":"A property mapping with the same name already exists\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsFormsIntegration/System/Windows/Integration/PropertyMap.cs","lineNumber":128,"sourceCode":"        public ICollection Values\n        {\n            get\n            {\n                return _wrappedDictionary.Values;\n            }\n        }\n\n        /// <summary>\n        ///     Adds a PropertyTranslator delegate to the property map that runs when the specified property \n        ///     of the host control changes. \n        /// </summary>\n        /// <param name=\"propertyName\">A string containing the name of the host control property to translate.</param>\n        /// <param name=\"translator\">A PropertyTranslator delegate which will be called when the specificied property changes.</param>\n        public void Add(String propertyName, PropertyTranslator translator)\n        {\n            if (Contains(propertyName))\n            {\n                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, SR.WFI_PropertyMappingExists, propertyName));\n            }\n            this[propertyName] = translator;\n        }\n\n        private void ThrowIfPropertyDoesntExistOnSource(string propertyName)\n        {\n            if (SourceObject != null)\n            {\n                if (GetProperty(propertyName) == null)\n                {\n                    // Property 'Foreground' doesn't exist on type 'Window'\n                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SR.WFI_PropertyDoesntExist, propertyName, SourceObject.GetType().FullName));\n                }\n            }\n        }\n\n        /// <summary>\n        ///     Runs the PropertyTranslator for the given property, based on the ","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsFormsIntegration/System/Windows/Integration/PropertyMap.cs#L110-L146","documentation":"PropertyMap.Add rejects duplicate mappings: if Contains(propertyName) is true it throws InvalidOperationException with SR.WFI_PropertyMappingExists ('A property mapping with the same name already exists.'). Add is for new entries only; replacing an existing translation must go through the indexer, which is designed to overwrite.","triggerScenarios":"Calling Add(\"Background\", translator) twice, or Add on a name already set by the default PropertyMap (WFI-provided defaults may already contain common properties like Background, FontFamily, etc.).","commonSituations":"Initialization code running twice (page/control reload, repeated user-control instantiation); assuming a fresh PropertyMap when defaults already populated entries; plugins each registering translators for the same property.","solutions":["Use the indexer host.PropertyMap[propertyName] = translator to replace an existing mapping — the SOURCE shows Add delegates to it and it allows overwrite.","Check Contains(propertyName) first and choose Add vs. indexer assignment accordingly.","Guard initialization so translators are registered only once (e.g. an IsInitialized flag).","If registering many mappings, wrap Add in a check or wrap in try/catch on InvalidOperationException to skip duplicates deliberately."],"exampleFix":"// before\nhost.PropertyMap.Add(\"Background\", TranslateBackground); // second call throws\n// after\nhost.PropertyMap[\"Background\"] = TranslateBackground; // replaces existing mapping","handlingStrategy":"validation","validationCode":"if (host.PropertyMap.Contains(propertyName))\n    host.PropertyMap[propertyName] = translator; // overwrite\nelse\n    host.PropertyMap.Add(propertyName, translator);","typeGuard":"static bool MappingExists(PropertyMap map, string name) => map.Contains(name);","tryCatchPattern":"try\n{\n    host.PropertyMap.Add(propertyName, translator);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already exists\"))\n{\n    logger.LogWarning(ex, \"Mapping for '{Property}' already exists; overwriting\", propertyName);\n    host.PropertyMap[propertyName] = translator;\n}","preventionTips":["Guard initialization with a flag so translators are registered once per control lifetime.","Remember default WFI PropertyMaps pre-populate common properties — check Contains first.","Use the indexer when you intend replace semantics; reserve Add for genuinely new mappings."],"tags":["wpf","winforms-integration","propertymap","duplicate-key"],"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"}