SetValue Method
Collapse the table of content
Expand the table of content

DependencyObject.SetValue Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Sets the local value of a dependency property on a DependencyObject.

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

Public Sub SetValue ( _
	dp As DependencyProperty, _
	value As Object _


Type: System.Windows.DependencyProperty
The identifier of the dependency property to set.
Type: System.Object
The new local value.


value was not the correct type as registered for the dp property.

If the provided type does not match the type that is declared for the dependency property as it was originally registered, an exception is thrown. The value parameter should always be provided as the appropriate type.

In contrast, to use the managed code version of SetValue, 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 that owns the dependency property itself. Note that not all properties are dependency properties, which means that the managed code version of SetValue cannot be used to access all possible properties in the managed API for Windows Phone(you can only access dependency properties).

For user code, calling SetValue is not typically necessary. Usually, a Windows Phone property or a custom dependency property has a CLR property that wraps it, and you can just set the property value through a conventional CLR usage. There are three notable cases where SetValue is used more typically:

  • You are defining a custom dependency property. You will call SetValue as part of defining your own property set accessor for the CLR usage.

  • You are defining a callback or are in some other scope where you are already being passed a DependencyProperty identifier, and 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 SetValue, passing the identifier.

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

If the value on a property changes in the middle of an active animation, that SetValue  call will modify the base value of the animation. When the animation is stopped, it will return to the new base value set during the animation. This matches the behavior in WPF.

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

public class Fish : Control
    public static readonly DependencyProperty SpeciesProperty =
    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

© 2018 Microsoft