dotnet/wpf · error · NotSupportedException

NotSupportedException

Error message

NotSupportedException

What it means

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.

Solutions

  1. In the markup extension, feature-detect: only use TargetObject when the provider actually supports it.
  2. Restructure the extension to work from TargetProperty alone, or defer logic until the value is applied.
  3. Wrap the TargetObject access in try/catch for NotSupportedException and fall back.
  4. Avoid using the extension in template/BAML paths where TargetObject is unavailable; apply values in code instead.

Example fix

// before
var target = ((IProvideValueTarget)sp.GetService(typeof(IProvideValueTarget))).TargetObject;
// after
var ivt = sp.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
object target = null;
try { target = ivt?.TargetObject; } catch (NotSupportedException) { /* unavailable in this context */ }
Defensive patterns

Strategy: try-catch

Validate before calling

var ivt = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
bool hasTargetObject = ivt != null && !(ivt is WpfXamlMember); // WpfXamlMember only supplies TargetProperty

Try / catch

try { target = ((IProvideValueTarget)sp.GetService(typeof(IProvideValueTarget))).TargetObject; }
catch (NotSupportedException)
{
    // TargetObject unavailable (BAML/deferred path); degrade gracefully
}

Prevention

When it happens

Trigger: A markup extension's ProvideValue casts its IServiceProvider to IProvideValueTarget and reads TargetObject while the service is a WpfXamlMember that only exposes TargetProperty.

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

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/1956dc664ee38f53. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfXamlMember.cs:283

            get { return WpfXamlType.GetFlag(ref _bitField, (byte)BoolMemberBits.UseV3Rules); }
            set { WpfXamlType.SetFlag(ref _bitField, (byte)BoolMemberBits.UseV3Rules, value); }
        }
        private bool _isBamlMember
        {
            get { return WpfXamlType.GetFlag(ref _bitField, (byte)BoolMemberBits.BamlMember); }
            set { WpfXamlType.SetFlag(ref _bitField, (byte)BoolMemberBits.BamlMember, value); }
        }
        private bool _underlyingMemberIsKnown
        {
            get { return WpfXamlType.GetFlag(ref _bitField, (byte)BoolMemberBits.UnderlyingMemberIsKnown); }
            set { WpfXamlType.SetFlag(ref _bitField, (byte)BoolMemberBits.UnderlyingMemberIsKnown, value); }
        }

        #region IProvideValueTarget Members

        object System.Windows.Markup.IProvideValueTarget.TargetObject
        {
            get { throw new NotSupportedException(); }
        }

        object System.Windows.Markup.IProvideValueTarget.TargetProperty
        {
            get
            {
                if (DependencyProperty != null)
                {
                    return DependencyProperty;
                }
                else
                {
                    return UnderlyingMember;
                }
            }
        }

        #endregion

View on GitHub (pinned to 81131a70a4)