dotnet/wpf · error · System.InvalidOperationException

SR.MarkupExtensionProperty (TemplateBindingExtension…

Error message

SR.MarkupExtensionProperty (TemplateBindingExtension requires the Property to be set)

What it means

TemplateBindingExtension.ProvideValue creates a TemplateBindingExpression from the markup extension, but a template binding is meaningless without a target DependencyProperty. If the Property property is null, ProvideValue throws InvalidOperationException (SR.MarkupExtensionProperty).

Solutions

  1. Set the Property in XAML: use {TemplateBinding Background} instead of {TemplateBinding}.
  2. When creating the extension in code, assign Property (a DependencyProperty) before calling ProvideValue.
  3. Verify the XAML compiles/parses — malformed markup extension usage is the usual root cause.

Example fix

// before
var ext = new TemplateBindingExtension();
var value = ext.ProvideValue(serviceProvider);
// after
var ext = new TemplateBindingExtension();
ext.Property = Control.BackgroundProperty;
var value = ext.ProvideValue(serviceProvider);
Defensive patterns

Strategy: validation

Validate before calling

if (ext == null || ext.Property == null) throw new InvalidOperationException("TemplateBindingExtension.Property must be set before ProvideValue");

Type guard

static bool IsUsable(TemplateBindingExtension ext) => ext?.Property != null;

Try / catch

try { value = ext.ProvideValue(sp); }
catch (InvalidOperationException ex) { log(ex.Message); value = DependencyProperty.UnsetValue; }

Prevention

When it happens

Trigger: Using {TemplateBinding ...} in XAML without specifying the Property, or constructing new TemplateBindingExtension() and calling ProvideValue before assigning Property.

Common situations: Hand-written or programmatically generated XAML where the template binding lacks its property argument, e.g. {TemplateBinding} with no value, or a template applied outside a ControlTemplate where binding resolution fails.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateBindingExtension.cs:57

            ArgumentNullException.ThrowIfNull(property);

            _property = property;
        }

        /// <summary>
        ///  Return an object that should be set on the targetObject's targetProperty
        ///  for this markup extension.  For TemplateBindingExtension, this is the object found in
        ///  a resource dictionary in the current parent chain that is keyed by ResourceKey
        /// </summary>
        /// <param name="serviceProvider">ServiceProvider that can be queried for services.</param>
        /// <returns>
        ///  The object to set on this property.
        /// </returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Property == null)
            {
                throw new InvalidOperationException(SR.MarkupExtensionProperty);
            }

            return new TemplateBindingExpression(this);
        }

        /// <summary>
        ///     Property we are binding to
        /// </summary>
        [ConstructorArgument("property")]
        public DependencyProperty Property
        {
            get { return _property; }
            set
            {
                ArgumentNullException.ThrowIfNull(value);
                _property = value;
            }
        }

View on GitHub (pinned to 81131a70a4)