Экспорт (0) Печать
Развернуть все
EN
Данное содержимое не доступно на вашем языке, используйте версию на английском языке.

DependencyObject.SetValue Method

July 28, 2014

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

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

public void SetValue(
	DependencyProperty dp,
	Object value
)

Parameters

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

ExceptionCondition
ArgumentException

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 =
    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

Показ:
© 2014 Microsoft