Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

PropertyMetadata Class

Defines certain behavior aspects of a dependency property as it is applied to a specific type, including conditions it was registered with.

Namespace:  System.Windows
Assembly:  WindowsBase (in WindowsBase.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
public class PropertyMetadata
This type, and members of this type, are not typically used in XAML.

The PropertyMetadata type exposes the following members.

  NameDescription
Public methodPropertyMetadata() Initializes a new instance of the PropertyMetadata class.
Public methodPropertyMetadata(Object) Initializes a new instance of the PropertyMetadata class with a specified default value for the dependency property that this metadata will be applied to.
Public methodPropertyMetadata(PropertyChangedCallback) Initializes a new instance of the PropertyMetadata class with the specified PropertyChangedCallback implementation reference.
Public methodPropertyMetadata(Object, PropertyChangedCallback) Initializes a new instance of the PropertyMetadata class with the specified default value and PropertyChangedCallback implementation reference.
Public methodPropertyMetadata(Object, PropertyChangedCallback, CoerceValueCallback)Initializes a new instance of the PropertyMetadata class with the specified default value and callbacks.
Top
  NameDescription
Public propertyCoerceValueCallbackGets or sets a reference to a CoerceValueCallback implementation specified in this metadata.
Public propertyDefaultValue Gets or sets the default value of the dependency property.
Protected propertyIsSealedGets a value that determines whether the metadata has been applied to a property in some way, resulting in the immutable state of that metadata instance.
Public propertyPropertyChangedCallbackGets or sets a reference to a PropertyChangedCallback implementation specified in this metadata.
Top
  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMergeMerges this metadata with the base metadata.
Protected methodOnApplyCalled when this metadata has been applied to a property, which indicates that the metadata is being sealed.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

Property metadata can be defined and used during dependency property registration when calling the Register method (or variations for attached properties or read-only dependency properties), or after original owner registration when calling the OverrideMetadata method. AddOwner also takes property metadata.

This class is a concrete base class that can be used in each of these calls. However, it is very common to specify metadata using one of the derived classes such as FrameworkPropertyMetadata. These derived classes support more detailed metadata carried as Boolean property values, which are useful for either detecting or enabling some property system and layout behaviors that are only implemented at the WPF framework level.

Several properties of this class are read-write to the object model, but can only be written prior to the instance being used in a property system operation such as Register or OverrideMetadata. Each of these properties could also have been set by the constructor but are exposed so that Merge method implementations can set them.

This example shows how to back a common language runtime (CLR) property with a DependencyProperty field, thus defining a dependency property. When you define your own properties and want them to support many aspects of Windows Presentation Foundation (WPF) functionality, including styles, data binding, inheritance, animation, and default values, you should implement them as a dependency property.

The following example first registers a dependency property by calling the Register method. The name of the identifier field that you use to store the name and characteristics of the dependency property must be the Name you chose for the dependency property as part of the Register call, appended by the literal string Property. For instance, if you register a dependency property with a Name of Location, then the identifier field that you define for the dependency property must be named LocationProperty.

In this example, the name of the dependency property and its CLR accessor is State; the identifier field is StateProperty; the type of the property is Boolean; and the type that registers the dependency property is MyStateControl.

If you fail to follow this naming pattern, designers might not report your property correctly, and certain aspects of property system style application might not behave as expected.

You can also specify default metadata for a dependency property. This example registers the default value of the State dependency property to be false.

public class MyStateControl : ButtonBase
{
  public MyStateControl() : base() { }
  public Boolean State
  {
    get { return (Boolean)this.GetValue(StateProperty); }
    set { this.SetValue(StateProperty, value); } 
  }
  public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
    "State", typeof(Boolean), typeof(MyStateControl),new PropertyMetadata(false));
}

For more information about how and why to implement a dependency property, as opposed to just backing a CLR property with a private field, see Dependency Properties Overview.

More Code

How to: Override Metadata for a Dependency PropertyThis example shows how to override default dependency property metadata that comes from an inherited class, by calling the OverrideMetadata method and providing type-specific metadata.
How to: Register an Attached PropertyThis example shows how to register an attached property and provide public accessors so that you can use the property in both Extensible Application Markup Language (XAML) and code. Attached properties are a syntax concept defined by Extensible Application Markup Language (XAML). Most attached properties for WPF types are also implemented as dependency properties. You can use dependency properties on any DependencyObject types.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.