다음을 통해 공유


DependencyObject.GetValue Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Function GetValue ( _
    dp As DependencyProperty _
) As Object
public Object GetValue(
    DependencyProperty dp
)

Parameters

Return Value

Type: System.Object
Returns the current effective value.

Exceptions

Exception Condition
ArgumentNullException

dp is null.

Remarks

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 Silverlight 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. For details, see Custom Dependency Objects and Dependency Properties.

  • You are operating within the scope of API that exposes the lower levels of the Silverlight 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.

JavaScript API Notes

The JavaScript implementation of GetValue is notably different, primarily because its input type is treated as a name (a string), not as a DependencyProperty identifier (an object). You can use the JavaScript GetValue method to get the value of any Silverlight property that is available to the JavaScript API. However, you generally do not need this method to get property values, because all Silverlight properties support a more direct "dotted" property syntax in JavaScript access to the object model.

In contrast, to use the managed code implementation of GetValue, you must provide input of a DependencyProperty object. You would typically get this value from the public static property that serves as a dependency property identifier, available on the same object where the dependency property itself is a declared member. Note that not all properties are dependency properties, which means that the managed code implementation of GetValue cannot be used to access all possible properties in the managed API (you can only access dependency properties).

Examples

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); }
    }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.