{"record":{"id":"f2afa2d91cab4aab","repo":"dotnet/wpf","slug":"an-error-occurred-while-mapping-a-property","errorCode":null,"errorMessage":"An error occurred while mapping a property.","messagePattern":"An error occurred while mapping a property\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsFormsIntegration/System/Windows/Integration/PropertyMap.cs","lineNumber":320,"sourceCode":"        //In this specific case, we are catching the exception and firing an event which can be handled in user code\n        //The user handling the event can make the determination on whether or not the exception should be rethrown\n        //(Wrapped in an InvalidOperationException).\n        internal void RunTranslator(PropertyTranslator translator, object host, string propertyName, object value)\n        {\n            try\n            {\n                translator(host, propertyName, value);\n            }\n            catch (Exception ex)\n            {\n                PropertyMappingExceptionEventArgs args = new PropertyMappingExceptionEventArgs(ex, propertyName, value);\n                if (_propertyMappingError != null)\n                {\n                    _propertyMappingError(SourceObject, args);\n                }\n                if (args.ThrowException)\n                {\n                    throw new InvalidOperationException(SR.WFI_PropertyMapError, ex);\n                }\n            }\n        }\n\n        private event EventHandler<PropertyMappingExceptionEventArgs> _propertyMappingError;\n\n        /// <summary>\n        /// Occurs when an exception is raised by a PropertyMap delegate.\n        /// </summary>\n        public event EventHandler<PropertyMappingExceptionEventArgs> PropertyMappingError\n        {\n            add { _propertyMappingError += value; }\n            remove { _propertyMappingError -= value; }\n        }\n    }\n}\n","sourceCodeStart":302,"sourceCodeEnd":337,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsFormsIntegration/System/Windows/Integration/PropertyMap.cs#L302-L337","documentation":"RunTranslator wraps the execution of a PropertyTranslator; when the translator delegate throws, PropertyMap raises the PropertyMappingError event and, if PropertyMappingExceptionEventArgs.ThrowException is true, rethrows as an InvalidOperationException with inner exception 'ex'. This lets hosts decide whether a mapping failure should crash or be logged.","triggerScenarios":"A custom PropertyTranslator added via propertyMap.Add(...) throws inside its delegate (e.g. casting the property value to the wrong type), and either the default behavior or an event handler sets ThrowException = true.","commonSituations":"Custom translators written for one control type applied to another; translator code assuming a non-null SourceObject or specific value type; exceptions in translators triggered during layout when WindowsFormsHost applies mappings.","solutions":["Read the InnerException to find the real failure inside the translator delegate.","Fix the translator's logic (type casts, null checks) so it does not throw.","Subscribe to the PropertyMappingError event to log/instead of throwing; do not set args.ThrowException = true.","Validate the property value type before the translator uses it."],"exampleFix":"// before\npropertyMap.Add(\"Background\", (o, e) => ((System.Windows.Controls.Control)o).Background = (System.Windows.Media.Brush)e.Value); // bad cast\n// after\npropertyMap.Add(\"Background\", (o, e) =>\n{\n    var ctl = o as System.Windows.Controls.Control;\n    if (ctl != null && e.Value is System.Windows.Media.Brush brush)\n        ctl.Background = brush;\n});","handlingStrategy":"try-catch","validationCode":"propertyMap.PropertyMappingError += (s, e) => { log.Error($\"Mapping failed for {e.PropertyName}\", e.Exception); e.ThrowException = false; };","typeGuard":null,"tryCatchPattern":"try { propertyMap.Apply(); }\ncatch (InvalidOperationException ex) when (ex.InnerException != null)\n{ log.Error(\"Property translation failed\", ex.InnerException); }","preventionTips":["Write translators defensively: null-check SourceObject and type-check the value","Always inspect InnerException — it carries the translator's real failure","Subscribe to PropertyMappingError to log instead of throwing in production"],"tags":["invalid-operation","property-map","wrapped-exception","delegate"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}