Open sandboxFocusImprove this doc

XAM050: Dependency Property Declared in a Type That Is Not a DependencyObject

Severity Error
Reported by PostSharp.Patterns.Xaml
Message [<Aspect>] cannot be applied to property <Property>: the declaring type <DeclaringType> does not derive from the DependencyObject class.

Cause

The DependencyPropertyAttribute aspect registers its target property with the WPF property system, which stores the value of a dependency property in the object that owns it. Only a DependencyObject can store such values, so the class that declares the property must derive from DependencyObject. The AttachedPropertyAttribute aspect has the same requirement for the class that declares the attached property.

PostSharp reports XAM050 on the property when its declaring class does not derive from DependencyObject, directly or through a base class such as FrameworkElement. <Aspect> is DependencyProperty or AttachedProperty.

In the following example, the ContactCard class has a Phone dependency property but does not derive from DependencyObject.

using System;
using System.Windows;
using PostSharp.Patterns.Xaml;

namespace Errors.XAM050_Error;

public class ContactCard
{
    // ContactCard does not derive from DependencyObject, which stores the values of dependency properties.
    [DependencyProperty]
    public string? Phone { get; set; }
}

public static class Program
{
    public static void Main()
    {
        var card = new ContactCard { Phone = "+1 555 0100" };
        Console.WriteLine( $"Phone: {card.Phone}" );

        DependencyProperty property = DependencyPropertyServices.GetDependencyProperty( typeof(ContactCard), nameof(ContactCard.Phone) );
        Console.WriteLine( $"Registered dependency property: {property.Name}" );
    }
}
error XAM050: XAM050_Error.cs(11,20): [DependencyProperty] cannot be applied to property Errors.XAM050_Error.ContactCard.Phone: the declaring type Errors.XAM050_Error.ContactCard does not derive from the DependencyObject class.

Resolution

Derive the declaring class from DependencyObject, or from a WPF class that derives from it, such as UIElement, FrameworkElement or Control. When the class cannot derive from DependencyObject, use a plain property instead, and add the NotifyPropertyChangedAttribute aspect to the class if a binding must observe its changes.

In the following example, ContactCard derives from DependencyObject, and the aspect registers the Phone dependency property.

using System;
using System.Windows;
using PostSharp.Patterns.Xaml;

namespace Errors.XAM050_Fixed;

// ContactCard derives from DependencyObject, which stores the values of dependency properties.
public class ContactCard : DependencyObject
{
    [DependencyProperty]
    public string? Phone { get; set; }
}

public static class Program
{
    public static void Main()
    {
        var card = new ContactCard { Phone = "+1 555 0100" };
        Console.WriteLine( $"Phone: {card.Phone}" );

        DependencyProperty property = DependencyPropertyServices.GetDependencyProperty( typeof(ContactCard), nameof(ContactCard.Phone) );
        Console.WriteLine( $"Registered dependency property: {property.Name}" );
    }
}
Phone: +1 555 0100
Registered dependency property: Phone

See Also

Reference

DependencyPropertyAttribute
AttachedPropertyAttribute
DependencyObject

Other Resources

Dependency Property
Attached Property