dotnet/wpf · error · XamlParseException

SR.MarkupExtensionDynamicOrBindingOnClrProp

Error message

SR.MarkupExtensionDynamicOrBindingOnClrProp

What it means

Helper.CheckCanReceiveMarkupExtension validates that a markup extension can be applied to a target member. When the target is a plain CLR property (not a DependencyProperty) and the member type does not accept the extension's type, it throws this XamlParseException naming the extension, member, and target type.

Solutions

  1. Convert the target property to a DependencyProperty (register with DependencyProperty.Register) so extensions can bind to it.
  2. Remove the markup extension and assign the value directly in code or via a setter.
  3. Ensure the property type is compatible with the extension (e.g. Binding targets must be dependency properties).
  4. Use the documented special cases (HierarchicalDataTemplate.ItemsSource, GridViewColumn.DisplayMemberBinding) only as designed.

Example fix

// before
public string Title { get; set; }  // XAML: Title="{Binding Path=Name}"
// after
public static readonly DependencyProperty TitleProperty =
    DependencyProperty.Register("Title", typeof(string), typeof(MyControl));
public string Title { get { return (string)GetValue(TitleProperty); } set { SetValue(TitleProperty, value); } }
Defensive patterns

Strategy: validation

Validate before calling

static bool CanReceiveMarkup(DependencyObject target, string prop, object ext)
{
    var pi = target.GetType().GetProperty(prop);
    if (pi == null) return false;
    return pi.GetValue(target) is DependencyProperty backerOrDp == false
        ? typeof(MarkupExtension).IsAssignableFrom(pi.PropertyType) &&
          pi.PropertyType.IsInstanceOfType(ext)
        : true;
}

Try / catch

try { Helper.CheckCanReceiveMarkupExtension(target, propertyInfo, ext); }
catch (XamlParseException ex) { log.Error($"Extension {ex.Message} not allowed on CLR property"); }

Prevention

When it happens

Trigger: Applying a Binding/StaticResource/DynamicResource-style extension to a non-dependency CLR property whose type is not MarkupExtension-compatible, e.g. a Binding on a plain C# property set in XAML where the property type is not assignable from the extension.

Common situations: Adding bindings to view-model-facing CLR properties in XAML, custom controls exposing plain properties expecting dynamic values, and migrating WinForms-style properties to XAML without converting them to dependency properties.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Helper.cs:664

                        else
                        {
                            MethodInfo methodInfo = (MethodInfo)targetMember;
                            ParameterInfo[] parameterInfos = methodInfo.GetParameters();
                            Debug.Assert(parameterInfos.Length == 2, "The signature of a static settor must contain two parameters");
                            memberType = parameterInfos[1].ParameterType;
                        }

                        // Check if the MarkupExtensionType is assignable to the given MemberType
                        // This check is to allow properties such as the following
                        // - DataTrigger.Binding
                        // - Condition.Binding
                        // - HierarchicalDataTemplate.ItemsSource
                        // - GridViewColumn.DisplayMemberBinding

                        if (!typeof(MarkupExtension).IsAssignableFrom(memberType) ||
                             !memberType.IsAssignableFrom(markupExtension.GetType()))
                        {
                            throw new XamlParseException(SR.Format(SR.MarkupExtensionDynamicOrBindingOnClrProp,
                                                                markupExtension.GetType().Name,
                                                                targetMember.Name,
                                                                targetType.Name));
                        }
                    }
                    else
                    {
                        // This is the Collection ContentProperty case
                        // Example:
                        // <DockPanel>
                        //   <Button />
                        //   <DynamicResource ResourceKey="foo" />
                        // </DockPanel>

                        // Collection<BindingBase> used in MultiBinding is a special
                        // case of a Collection that can contain a Binding.

                        if (!typeof(BindingBase).IsAssignableFrom(markupExtension.GetType()) ||

View on GitHub (pinned to 81131a70a4)