DependencyProperty.GetMetadata Method

July 28, 2014

Retrieves the property metadata value for the dependency property as registered to the specified Type.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

public PropertyMetadata GetMetadata(
	Type forType
)

Parameters

forType
Type: System.Type
The type for which to retrieve the dependency property metadata.

Return Value

Type: System.Windows.PropertyMetadata
A property metadata object.

In WPF, specifying either the type or an object reference to obtain a type from is necessary because the metadata can vary from the original registration. (This is because WPF supports AddOwner or OverrideMetadata calls that alter the metadata of the dependency property as it exists on a type.) Windows Phone does not support a similar AddOwner or OverrideMetadata technique. However, the Windows Phone  implementation of GetMetadata still requires a type to be passed for compatibility reasons. To get a value to use as forType, you can either call typeof() for the known owner type of the dependency property, or use an available type reference you have obtained by some other means.

The most common scenario for getting a PropertyMetadata value is to check the DefaultValue. That is the only public property available on the PropertyMetadata type in Windows Phone.

The following example implements a utility method that reports the default value of a given dependency property as it exists in FrameworkElement, based on the default value registered and stored in the metadata:


public static object GetDefaultValueForFrameworkDP(DependencyProperty dpIdentifier)
{
    PropertyMetadata metadataInfo = dpIdentifier.GetMetadata(typeof(FrameworkElement));
    return metadataInfo.DefaultValue;
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft