Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DependencyProperty.RegisterReadOnly Method (String, Type, Type, PropertyMetadata)

Registers a read-only dependency property, with the specified property type, owner type, and property metadata.

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

'Declaration
Public Shared Function RegisterReadOnly ( _
	name As String, _
	propertyType As Type, _
	ownerType As Type, _
	typeMetadata As PropertyMetadata _
) As DependencyPropertyKey
'Usage
Dim name As String
Dim propertyType As Type
Dim ownerType As Type
Dim typeMetadata As PropertyMetadata
Dim returnValue As DependencyPropertyKey

returnValue = DependencyProperty.RegisterReadOnly(name, propertyType, ownerType, typeMetadata)
public static DependencyPropertyKey RegisterReadOnly (
	String name, 
	Type propertyType, 
	Type ownerType, 
	PropertyMetadata typeMetadata
)
public static function RegisterReadOnly (
	name : String, 
	propertyType : Type, 
	ownerType : Type, 
	typeMetadata : PropertyMetadata
) : DependencyPropertyKey
You cannot use methods in XAML.

Parameters

name

The name of the dependency property to register.

propertyType

The type of the property.

ownerType

The owner type that is registering the dependency property.

typeMetadata

Property metadata for the dependency property.

Return Value

A dependency property key that should be used to set the value of a static read-only field in your class, which is then used to reference the dependency property.

This method returns the type DependencyPropertyKey, whereas RegisterAttached returns the type DependencyProperty. Typically, the keys that represent read-only properties are not made public, because the keys can be used to set the dependency property value by calling SetValue(DependencyPropertyKey,Object). Your class design will affect your requirements, but it is generally recommended to limit the access and visibility of any DependencyPropertyKey to only those parts of your code that are necessary to set that dependency property as part of class or application logic. It is also recommended that you expose a dependency property identifier for the read-only dependency property, by exposing the value of DependencyPropertyKey.DependencyProperty as a public static readonly field on your class.

Read-only dependency properties are a fairly typical scenario. You can use the value of a read-only dependency property as the basis for other property system operations that take a dependency property, such as basing a Trigger on the dependency property in a style.

For more information on dependency property registration, see DependencyProperty.

The following example registers an AquariumSize dependency property as read-only. The example defines AquariumSizeKey as an internal key (so that other classes in the assembly could override metadata) and exposes the dependency property identifier based on that key as AquariumSizeProperty. Also, a wrapper is created for AquariumSize, with only a get accessor.

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft