{"record":{"id":"1956dc664ee38f53","repo":"dotnet/wpf","slug":"notsupportedexception-wpfxamlmember","errorCode":null,"errorMessage":"NotSupportedException","messagePattern":"NotSupportedException","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfXamlMember.cs","lineNumber":283,"sourceCode":"            get { return WpfXamlType.GetFlag(ref _bitField, (byte)BoolMemberBits.UseV3Rules); }\n            set { WpfXamlType.SetFlag(ref _bitField, (byte)BoolMemberBits.UseV3Rules, value); }\n        }\n        private bool _isBamlMember\n        {\n            get { return WpfXamlType.GetFlag(ref _bitField, (byte)BoolMemberBits.BamlMember); }\n            set { WpfXamlType.SetFlag(ref _bitField, (byte)BoolMemberBits.BamlMember, value); }\n        }\n        private bool _underlyingMemberIsKnown\n        {\n            get { return WpfXamlType.GetFlag(ref _bitField, (byte)BoolMemberBits.UnderlyingMemberIsKnown); }\n            set { WpfXamlType.SetFlag(ref _bitField, (byte)BoolMemberBits.UnderlyingMemberIsKnown, value); }\n        }\n\n        #region IProvideValueTarget Members\n\n        object System.Windows.Markup.IProvideValueTarget.TargetObject\n        {\n            get { throw new NotSupportedException(); }\n        }\n\n        object System.Windows.Markup.IProvideValueTarget.TargetProperty\n        {\n            get\n            {\n                if (DependencyProperty != null)\n                {\n                    return DependencyProperty;\n                }\n                else\n                {\n                    return UnderlyingMember;\n                }\n            }\n        }\n\n        #endregion","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfXamlMember.cs#L265-L301","documentation":"WpfXamlMember implements IProvideValueTarget for markup-extension evaluation. TargetObject is intentionally unsupported in this context and always throws NotSupportedException; only TargetProperty (the DependencyProperty or underlying member) is available. This surfaces when a markup extension reads TargetObject during BAML2006 member processing where no target object is provided.","triggerScenarios":"A markup extension's ProvideValue casts its IServiceProvider to IProvideValueTarget and reads TargetObject while the service is a WpfXamlMember that only exposes TargetProperty.","commonSituations":"Custom markup extensions that depend on TargetObject (e.g. to inspect the owning control) used in deferred content or BAML load paths where WPF cannot supply a live target; template usage where ProvideValue runs without a concrete target instance.","solutions":["In the markup extension, feature-detect: only use TargetObject when the provider actually supports it.","Restructure the extension to work from TargetProperty alone, or defer logic until the value is applied.","Wrap the TargetObject access in try/catch for NotSupportedException and fall back.","Avoid using the extension in template/BAML paths where TargetObject is unavailable; apply values in code instead."],"exampleFix":"// before\nvar target = ((IProvideValueTarget)sp.GetService(typeof(IProvideValueTarget))).TargetObject;\n// after\nvar ivt = sp.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;\nobject target = null;\ntry { target = ivt?.TargetObject; } catch (NotSupportedException) { /* unavailable in this context */ }","handlingStrategy":"try-catch","validationCode":"var ivt = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;\nbool hasTargetObject = ivt != null && !(ivt is WpfXamlMember); // WpfXamlMember only supplies TargetProperty","typeGuard":null,"tryCatchPattern":"try { target = ((IProvideValueTarget)sp.GetService(typeof(IProvideValueTarget))).TargetObject; }\ncatch (NotSupportedException)\n{\n    // TargetObject unavailable (BAML/deferred path); degrade gracefully\n}","preventionTips":["Design markup extensions to work without TargetObject.","Null/feature-check IProvideValueTarget before relying on TargetObject.","Test extensions in both direct-XAML and template/deferred scenarios."],"tags":["wpf","xaml","markup-extension","not-supported"],"backgroundTag":"operation-not-supported","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"}