This is the online documentation for PostSharp 5.0.
Download PDF or CHM. Go to v4.3 or v5.0

Dependency Property

XAML dependency properties extend the functionality of the CLR properties with features such as data binding, styling, animation, etc. Whenever you need to define a dependency property in your XAML application, you typically have to follow a strict implementation pattern and write a fair amount of boilerplate code. The DependencyPropertyAttribute aspect allows you to create custom dependency properties much faster, without writing repetitive code.

This topic contains the following sections:

Creating a simple dependency property

To add a new dependency property to your class

  1. Add a new property to your class with a chosen name, type and a public getter and setter.

  2. Mark your new property with the DependencyPropertyAttribute attribute.

After you have marked your property with the DependencyPropertyAttribute attribute, you can immediately start using advanced features in your XAML code.

Example

The following code snippet comes from a custom control named ContactCard. The snippet defines a dependency property named Phone. The custom control defines other dependency properties not listed here.

C#
[DependencyProperty]
public string Phone { get; set; }

The dependency properties can be used in XAML for advanced features. In the following code snippet, we're using data binding with the dependency properties of the ContactCard custom control.

XAML
<local:ContactCard FullName="{Binding ContactName}" Phone="{Binding ContactPhone}" Email="{Binding ContactEmail}" Notes="{Binding ContactNotes}"
                   x:Name="CurrentContactCard" HorizontalAlignment="Left" Margin="478,270,0,0" VerticalAlignment="Top" Background="Bisque"/>
Exposing the DependencyProperty object

If you want to manipulate the dependency property using the XAML API in C# or VB, then you need to get the DependencyProperty for this specific property. One solution is to use the GetDependencyProperty(Type, String) method, but this is long and error-prone.

A better solution is to add a public static property of type DependencyProperty with the same name as your dependency property, but the Property suffix. This property will be picked by the DependencyPropertyAttribute aspect and set to the proper DependencyProperty value.

Example

The following example shows how to expose the DependencyProperty for the Phone property.

C#
[DependencyProperty]
public string Phone { get; set; }

public static DependencyProperty PhoneProperty { get; private set; }
C#
this.CurrentContactCard.SetBinding(ContactCard.PhoneProperty, new Binding("ContactPhone"));
Validating the value of the dependency property with contracts

PostSharp Code Contracts (see Contracts) provide a convenient way to validate the values of the dependency properties. To add the validation to your dependency property, you just need to apply a contract attribute to that property.

Example

The following code snippet shows how to validate a dependency property using a code contract.

C#
[DependencyProperty]
[NotEmpty]
public string FullName { get; set; }
Validating the value of the dependency property with a validation method

If you need more complex validation for your dependency property, you can implement it in a dedicated validation method. To define a validation method for the Email dependency property, add a new method named ValidateEmail to the same class where the property is declared. The method must accept one argument with the type assignable from the property type and return a bool value.

The following list shows the method signatures you can use when implementing the validation method where TPropertyType is the type of the dependency property and TDeclaringType is the class where your property is declared.

  • static bool ValidatePropertyName(TPropertyType value)

  • static bool ValidatePropertyName(DependencyProperty property, TPropertyType value)

  • static bool ValidatePropertyName(TDeclaringType instance, TPropertyType value)

  • static bool ValidatePropertyName(DependencyProperty property, TDeclaringType instance, TPropertyType value)

  • bool ValidatePropertyName(TPropertyType value)

  • bool ValidatePropertyName(DependencyProperty property, TPropertyType value)

Example

The following code snippet shows how to validate a dependency property using a validation method.

C#
[DependencyProperty]
public string Email { get; set; }

private bool ValidateEmail(string value)
{
    return EmailRegex.IsMatch(value);
}
Reacting to the changes of the dependency property value

The WPF property system can automatically notify you about the dependency property value changes via callback methods. This can be useful when, for example, you need to update the visual presentation of your custom UI control in response to a change of its property. This section shows how you can define a property change callback method with the PostSharp's dependency property pattern.

To define a property change callback method for the PictureUrl dependency property, add a new method named OnPictureUrlChanged to the same class where the property is declared. The method doesn't have to accept any arguments and must have a void return type. Implement your property change handling logic inside this new method.

The following list shows the method signatures you can use when implementing the property change callback method. TDeclaringType is the class where your property is declared.

  • static void OnPropertyNameChanged()

  • static void OnPropertyNameChanged(DependencyProperty property)

  • static void OnPropertyNameChanged(TDeclaringType instance)

  • static void OnPropertyNameChanged(DependencyProperty property, TDeclaringType instance)

  • void OnPropertyNameChanged()

  • void OnPropertyNameChanged(DependencyProperty property)

Example

C#
[DependencyProperty]
public string PictureUrl { get; set; }

private void OnPictureUrlChanged()
{
    this.ProfileImage.Source = this.LoadImageFromUrl(this.PictureUrl);
}
Implementing INotifyPropertyChanged

You may also want to notify the users of your class when a dependency property value changes. In this case, you would normally need to implement the INotifyPropertyChanged interface in your class and raise the PropertyChanged event. PostSharp helps you to automate this task using INotifyPropertyChanged pattern. To raise the PropertyChanged event every time any of the dependency properties in your class changes its value, mark your class with the NotifyPropertyChangedAttribute attribute.

C#
[NotifyPropertyChanged]
public partial class ContactCard : UserControl
{
    // ...
Overriding the naming conventions

The DependencyPropertyAttribute aspect follows a predefined naming convention when looking for methods and properties associated with the dependency property in your class. You can override the naming convention and choose your own member names by setting properties on the DependencyPropertyAttribute. The following table shows the default naming convention and the properties used to override member names.

Dependency property pattern's naming conventions

Member kind

Default name

Example

Override property

Value validation method

ValidatePropertyName

ValidatePhoneNumber

ValidateValueMethod

Property changed callback method

OnPropertyNameChanged

OnPhoneNumberChanged

PropertyChangedMethod

Registration property

PropertyNameProperty

PhoneNumberProperty

RegistrationProperty

Example

C#
[DependencyProperty(ValidateValueMethod = "ValidateStringMaxLength" )]
public string Notes { get; set; }

private bool ValidateStringMaxLength(string value)
{
    if (string.IsNullOrEmpty(value))
        return true;

    return value.Length <= MAX_LENGTH;
}
See Also