{"record":{"id":"79effa7383380c62","repo":"dotnet/wpf","slug":"sr-textrange-invalidparametervalue-79effa","errorCode":null,"errorMessage":"SR.TextRange_InvalidParameterValue","messagePattern":"SR\\.TextRange_InvalidParameterValue","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/EventSetterHandlerConverter.cs","lineNumber":62,"sourceCode":"        ///     Convert a string like \"Button.Click\" into the corresponding RoutedEvent\n        /// </summary>\n        public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext,\n                                           CultureInfo cultureInfo,\n                                           object source)\n        {\n            ArgumentNullException.ThrowIfNull(typeDescriptorContext);\n            ArgumentNullException.ThrowIfNull(source);\n            if (s_ServiceProviderContextType == null)\n            {\n                // get typeof(MS.Internal.Xaml.ServiceProviderContext) via reflection\n                Assembly a = typeof(IRootObjectProvider).Assembly;\n                s_ServiceProviderContextType = a.GetType(\"MS.Internal.Xaml.ServiceProviderContext\");\n            }\n            if (typeDescriptorContext.GetType() != s_ServiceProviderContextType)\n            {\n                // if the caller is not the XAML parser, don't answer.   This avoids\n                // returning an arbitrary delegate to a (possibly malicious) caller.\n                throw new ArgumentException(SR.TextRange_InvalidParameterValue, nameof(typeDescriptorContext));\n            }\n            IRootObjectProvider rootProvider = typeDescriptorContext.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;\n            if (rootProvider != null && source is String)\n            {\n                IProvideValueTarget ipvt = typeDescriptorContext.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;\n                if (ipvt != null)\n                {\n                    EventSetter setter = ipvt.TargetObject as EventSetter;\n                    string handlerName;\n                    if(setter != null && (handlerName = source as string) != null)\n                    {\n                        handlerName = handlerName.Trim();\n                        return Delegate.CreateDelegate(setter.Event.HandlerType, rootProvider.RootObject, handlerName);\n                    }\n                }\n            }\n\n            throw GetConvertFromException(source);","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/EventSetterHandlerConverter.cs#L44-L80","documentation":"EventSetterHandlerConverter.ConvertFrom converts XAML string handler names into delegates, but only when invoked by the XAML parser. It throws ArgumentException with TextRange_InvalidParameterValue when the typeDescriptorContext is not MS.Internal.Xaml.ServiceProviderContext, deliberately refusing to hand a delegate to arbitrary callers (security measure).","triggerScenarios":"Calling EventSetterHandlerConverter.ConvertFrom directly (or from custom tooling) with a plain ITypeDescriptorContext that is not the XAML parser's ServiceProviderContext.","commonSituations":"Custom XAML readers/designers, serialization utilities, or unit tests invoking the converter directly instead of letting the WPF XAML parser process the Handler attribute.","solutions":["Do not call this converter directly; let the WPF XAML parser process the EventSetter Handler attribute","In code, construct the delegate yourself: new RoutedEventHandler(OnClick)","If building tooling, replicate the parser behavior: resolve the method on the root object and create the delegate explicitly"],"exampleFix":"// before\nvar del = converter.ConvertFrom(ctx, culture, \"OnClick\"); // throws (ctx is not XAML context)\n// after\nvar del = new RoutedEventHandler(OnClick);","handlingStrategy":"type-guard","validationCode":"// Do not call this converter from application code; construct delegates directly.","typeGuard":"bool IsXamlParserContext(ITypeDescriptorContext ctx) =>\n    ctx?.GetType().FullName == \"MS.Internal.Xaml.ServiceProviderContext\";","tryCatchPattern":"try { result = converter.ConvertFrom(ctx, culture, handlerName); }\ncatch (ArgumentException ex) when (ex.ParamName == \"typeDescriptorContext\") { result = null; }","preventionTips":["Let the XAML parser handle Handler attributes","In code-behind, create delegates with new RoutedEventHandler(...) instead of converters"],"tags":["wpf","typeconverter","xaml","security"],"backgroundTag":"invalid-argument-value","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"}