DependencyObject.SetValue Method (DependencyPropertyKey, Object) (System.Windows)

Switch View :
ScriptFree
.NET Framework Class Library
DependencyObject.SetValue Method (DependencyPropertyKey, Object)

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.

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

Visual Basic
Public Sub SetValue ( _
	key As DependencyPropertyKey, _
	value As Object _
)
C#
public void SetValue(
	DependencyPropertyKey key,
	Object value
)
Visual C++
public:
void SetValue(
	DependencyPropertyKey^ key, 
	Object^ value
)
F#
member SetValue : 
        key:DependencyPropertyKey * 
        value:Object -> unit 

Parameters

key
Type: System.Windows.DependencyPropertyKey

The DependencyPropertyKey identifier of the property to set.

value
Type: System.Object

The new local value.

Remarks

This signature is generally used when you set values for read-only dependency properties that are defined by your custom classes. Generally, SetValue is called only from the type that registered that dependency property, which implements the internal logic that provides the determined value for the dependency property. For more information, see Read-Only Dependency Properties.

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. The exception conditions are potentially influenced by the ValidateValueCallback callback that exists on the dependency property identifier of the dependency property being set.

Examples

The following example defines a read-only dependency property, along with a public static readonly DependencyProperty that provides necessary read-only exposure to property consumers, and the get accessor for the CLR wrapper.

Visual Basic

Friend Shared ReadOnly AquariumSizeKey As DependencyPropertyKey = DependencyProperty.RegisterReadOnly("AquariumSize", GetType(Double), GetType(Aquarium), New PropertyMetadata(Double.NaN))
Public Shared ReadOnly AquariumSizeProperty As DependencyProperty = AquariumSizeKey.DependencyProperty
Public ReadOnly Property AquariumSize() As Double
    Get
        Return CDbl(GetValue(AquariumSizeProperty))
    End Get
End Property


C#

internal static readonly DependencyPropertyKey AquariumSizeKey = DependencyProperty.RegisterReadOnly(
  "AquariumSize",
  typeof(double),
  typeof(Aquarium),
  new PropertyMetadata(double.NaN)
);
public static readonly DependencyProperty AquariumSizeProperty =
  AquariumSizeKey.DependencyProperty;
public double AquariumSize
{
  get { return (double)GetValue(AquariumSizeProperty); }
}


Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8 Consumer Preview, Windows Server 8 Beta, Windows 7, Windows Server 2008 SP2, Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

See Also

Reference

Other Resources