dotnet/wpf · error · InvalidOperationException

A property mapping with the same name already exists.

Error message

A property mapping with the same name already exists.

What it means

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.

Solutions

  1. Use the indexer host.PropertyMap[propertyName] = translator to replace an existing mapping — the SOURCE shows Add delegates to it and it allows overwrite.
  2. Check Contains(propertyName) first and choose Add vs. indexer assignment accordingly.
  3. Guard initialization so translators are registered only once (e.g. an IsInitialized flag).
  4. If registering many mappings, wrap Add in a check or wrap in try/catch on InvalidOperationException to skip duplicates deliberately.

Example fix

// before
host.PropertyMap.Add("Background", TranslateBackground); // second call throws
// after
host.PropertyMap["Background"] = TranslateBackground; // replaces existing mapping
Defensive patterns

Strategy: validation

Validate before calling

if (host.PropertyMap.Contains(propertyName))
    host.PropertyMap[propertyName] = translator; // overwrite
else
    host.PropertyMap.Add(propertyName, translator);

Type guard

static bool MappingExists(PropertyMap map, string name) => map.Contains(name);

Try / catch

try
{
    host.PropertyMap.Add(propertyName, translator);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("already exists"))
{
    logger.LogWarning(ex, "Mapping for '{Property}' already exists; overwriting", propertyName);
    host.PropertyMap[propertyName] = translator;
}

Prevention

When it happens

Trigger: 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.).

Common situations: 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.

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/05bf49073dea7530. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsFormsIntegration/System/Windows/Integration/PropertyMap.cs:128

        public ICollection Values
        {
            get
            {
                return _wrappedDictionary.Values;
            }
        }

        /// <summary>
        ///     Adds a PropertyTranslator delegate to the property map that runs when the specified property 
        ///     of the host control changes. 
        /// </summary>
        /// <param name="propertyName">A string containing the name of the host control property to translate.</param>
        /// <param name="translator">A PropertyTranslator delegate which will be called when the specificied property changes.</param>
        public void Add(String propertyName, PropertyTranslator translator)
        {
            if (Contains(propertyName))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, SR.WFI_PropertyMappingExists, propertyName));
            }
            this[propertyName] = translator;
        }

        private void ThrowIfPropertyDoesntExistOnSource(string propertyName)
        {
            if (SourceObject != null)
            {
                if (GetProperty(propertyName) == null)
                {
                    // Property 'Foreground' doesn't exist on type 'Window'
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SR.WFI_PropertyDoesntExist, propertyName, SourceObject.GetType().FullName));
                }
            }
        }

        /// <summary>
        ///     Runs the PropertyTranslator for the given property, based on the 

View on GitHub (pinned to 81131a70a4)