DependencyObject.GetValue Method

May 11, 2014

Returns the current effective value of a dependency property from a DependencyObject.

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

public Object GetValue(
	DependencyProperty dp
)

Parameters

dp
Type: System.Windows.DependencyProperty
The DependencyProperty identifier of the property to retrieve the value for.

Return Value

Type: System.Object
Returns the current effective value.

ExceptionCondition
ArgumentNullException

dp is null.

The effective value is the value of the property that is returned by the property system to any caller that is requesting the value. The effective value is the result of the property system having evaluated all the possible inputs that participate in the property system value precedence. This includes property-changed callbacks, data binding, styles and templates, and animation.

For user code, calling GetValue is not typically necessary. Usually, a Windows Phone dependency property or a custom dependency property has a CLR property that wraps it, and you can get the property value through a simple and conventional CLR usage. There are three notable scenarios where GetValue might be used rather than CLR wrappers:

  • You are defining a custom dependency property yourself. You will call GetValue as part of defining your own property get accessor / wrapper for the CLR usage.

  • You are operating within the scope of API that exposes the lower levels of the Windows Phone property system. Such APIs often use a DependencyProperty value as a parameter or return value. For example, you might be defining a callback or are in some other scope where you are already being passed a DependencyProperty identifier. It is possible that more than one dependency property exists that you might want to interact with in that scope. In these cases it is probably simpler to call GetValue, passing the identifier you already have. 

  • The DependencyProperty identifier references an attached property (or you are not sure whether it is an attached property or a dependency property with a CLR wrapper). Attached properties also have dedicated get and set accessors per their XAML implementation pattern, which are typically more type-safe. But it still might be more convenient to use GetValue if you already have the identifier.

The following example shows a simple dependency property declaration. A call to GetValue constitutes the entirety of the get accessor implementation for the CLR wrapper of the new dependency property.


public class Fish : Control
{
    public static readonly DependencyProperty SpeciesProperty =
    DependencyProperty.Register(
    "Species",
    typeof(string),
    typeof(Fish), null
    );
    public string Species
    {
        get { return (string)GetValue(SpeciesProperty); }
        set { SetValue(SpeciesProperty, (string)value); }
    }
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft