Dependency Property Validation

WPF and Silverlight dependency properties (DP) can be tedious to write correctly. GammaXaml helps you with those by detecting at design time a number of common problems.

Every of the following highlighting has a configurable severity and can be disabled if needed.

Field modifiers

A valid dependency property field should be static and read-only.
  • Highlighting: ensure that a DependencyProperty field is static and read-only.
  • Quick fix: add static and/or readonly keywords to the field declaration.

Field name

A valid dependency property field should end with Property.
  • Highlighting: ensures that a DependencyProperty field ends with Property.
  • Highlighting: ensure that a DependencyPropertyKey field ends with PropertyKey (WPF only).
  • Quick fix: rename the field.

Property name

A valid dependency property field should have the same name (plus Property) as the property it represents.
  • Highlighting: ensure that a DependencyProperty.Register-like* call corresponds to the dependency property name.
  • Quick fix: rename the field.
  • Quick fix: change the property name in the call.

Accessor types

  • Highlighting: ensure that a DependencyProperty field declaration and its CLR accessor (whether it's a property in case of a normal DP, or a method in case of an attached DP) have the same type.
  • Quick fix: change the accessor type to match the DP type.
  • Quick fix: change the DP type in the Register call to match the accessor type.

Default value type

A default value for a dependency property, having a type that is different from the DP itself results in a runtime error. For example, it's quite easy to misuse 0 instead of 0.0 in a double DP.
  • Highlighting: ensure that a default value in a DP metadata in a Register-like* call matches the DP type.
  • Quick fix: change the default value to default(type).

Valid owner type

A non-attached dependency property must have a DependencyObject owner, otherwise a runtime error occurs.
  • Highlighting: ensure that an owner type in a Register-like* call is a DependencyObject.
  • Quick fix: make the owner type inherit from DependencyObject.

Possible wrong owner type

While it's not technically invalid to call Register or OverrideMetadata with an owner type different from the type containing the call, most of the time it indicates a potential problem. It's quite easy to copy and paste a Dependency Property from another type and forget to modify the owner type, which can results in a runtime error if the DP is already registered, or a silent failure otherwise. This kind of errors being quite frequent, GammaXaml warns you about it.
  • Highlighting: ensure that an owner type in a Register-like* call is the type containing the call.
  • Quick fix: change the owner type to the current type.


Remarks

  • Not every method is applicable to every validation type. For example, RegisterAttached accepts a non-DependencyObject as its owner (since Attached DP are often found on static classes), so its owner type won't be validated.
  • Those validations are available for both WPF and Silverlight (when applicable). However, there is no support for Windows Workflow Foundation DependencyProperty validation yet in GammaXaml.
  • This feature is currently only available in C# files, not Visual Basic files.

Last edited Jul 30, 2012 at 11:02 AM by MrJul, version 3

Comments

No comments yet.