DependencyProperty.Register 方法

定义

注册依赖属性。Registers a dependency property.

重载

Register(String, Type, Type)

使用指定的属性名称、属性类型和所有者类型注册依赖属性。Registers a dependency property with the specified property name, property type, and owner type.

Register(String, Type, Type, PropertyMetadata)

使用指定的属性名称、属性类型、所有者类型和属性元数据注册依赖属性。Registers a dependency property with the specified property name, property type, owner type, and property metadata.

Register(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的属性名称、属性类型、所有者类型、属性元数据和属性的值验证回叫来注册依赖属性。Registers a dependency property with the specified property name, property type, owner type, property metadata, and a value validation callback for the property.

Register(String, Type, Type)

使用指定的属性名称、属性类型和所有者类型注册依赖属性。Registers a dependency property with the specified property name, property type, and owner type.

public:
 static System::Windows::DependencyProperty ^ Register(System::String ^ name, Type ^ propertyType, Type ^ ownerType);
public static System.Windows.DependencyProperty Register (string name, Type propertyType, Type ownerType);
static member Register : string * Type * Type -> System.Windows.DependencyProperty
Public Shared Function Register (name As String, propertyType As Type, ownerType As Type) As DependencyProperty

参数

name
String

要注册的依赖属性的名称。The name of the dependency property to register. 名称必须在所有者类型的注册命名空间中是唯一的。The name must be unique within the registration namespace of the owner type.

propertyType
Type

属性的类型。The type of the property.

ownerType
Type

正在注册依赖属性的所有者类型。The owner type that is registering the dependency property.

返回

一个依赖属性标识符,应使用它来设置类中 public static readonly 字段的值。A dependency property identifier that should be used to set the value of a public static readonly field in your class. 稍后将此标识符用来引用依赖属性,从而实现以编程方式设置其值或获取元数据等操作。That identifier is then used to reference the dependency property later, for operations such as setting its value programmatically or obtaining metadata.

示例

public static readonly DependencyProperty IsDirtyProperty = DependencyProperty.Register(
  "IsDirty",
  typeof(Boolean),
  typeof(AquariumObject3)
);
Public Shared ReadOnly IsDirtyProperty As DependencyProperty = DependencyProperty.Register("IsDirty", GetType(Boolean), GetType(AquariumObject3))

注解

有关依赖属性注册的详细信息,请参阅 DependencyPropertyFor more information on dependency property registration, see DependencyProperty.

另请参阅

Register(String, Type, Type, PropertyMetadata)

使用指定的属性名称、属性类型、所有者类型和属性元数据注册依赖属性。Registers a dependency property with the specified property name, property type, owner type, and property metadata.

public:
 static System::Windows::DependencyProperty ^ Register(System::String ^ name, Type ^ propertyType, Type ^ ownerType, System::Windows::PropertyMetadata ^ typeMetadata);
public static System.Windows.DependencyProperty Register (string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata typeMetadata);
static member Register : string * Type * Type * System.Windows.PropertyMetadata -> System.Windows.DependencyProperty
Public Shared Function Register (name As String, propertyType As Type, ownerType As Type, typeMetadata As PropertyMetadata) As DependencyProperty

参数

name
String

要注册的依赖属性的名称。The name of the dependency property to register.

propertyType
Type

属性的类型。The type of the property.

ownerType
Type

正在注册依赖属性的所有者类型。The owner type that is registering the dependency property.

typeMetadata
PropertyMetadata

依赖属性的属性元数据。Property metadata for the dependency property.

返回

一个依赖属性标识符,应使用它来设置类中 public static readonly 字段的值。A dependency property identifier that should be used to set the value of a public static readonly field in your class. 稍后将此标识符用来引用依赖属性,从而实现以编程方式设置其值或获取元数据等操作。That identifier is then used to reference the dependency property later, for operations such as setting its value programmatically or obtaining metadata.

注解

有关依赖属性注册的详细信息,请参阅 DependencyPropertyFor more information on dependency property registration, see DependencyProperty.

另请参阅

Register(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的属性名称、属性类型、所有者类型、属性元数据和属性的值验证回叫来注册依赖属性。Registers a dependency property with the specified property name, property type, owner type, property metadata, and a value validation callback for the property.

public:
 static System::Windows::DependencyProperty ^ Register(System::String ^ name, Type ^ propertyType, Type ^ ownerType, System::Windows::PropertyMetadata ^ typeMetadata, System::Windows::ValidateValueCallback ^ validateValueCallback);
public static System.Windows.DependencyProperty Register (string name, Type propertyType, Type ownerType, System.Windows.PropertyMetadata typeMetadata, System.Windows.ValidateValueCallback validateValueCallback);
static member Register : string * Type * Type * System.Windows.PropertyMetadata * System.Windows.ValidateValueCallback -> System.Windows.DependencyProperty

参数

name
String

要注册的依赖属性的名称。The name of the dependency property to register.

propertyType
Type

属性的类型。The type of the property.

ownerType
Type

正在注册依赖属性的所有者类型。The owner type that is registering the dependency property.

typeMetadata
PropertyMetadata

依赖属性的属性元数据。Property metadata for the dependency property.

validateValueCallback
ValidateValueCallback

对回调的引用,除了典型的类型验证之外,该引用还应执行依赖属性值的任何自定义验证。A reference to a callback that should perform any custom validation of the dependency property value beyond typical type validation.

返回

一个依赖属性标识符,应使用它来设置类中 public static readonly 字段的值。A dependency property identifier that should be used to set the value of a public static readonly field in your class. 稍后将此标识符用来引用依赖属性,从而实现以编程方式设置其值或获取元数据等操作。That identifier is then used to reference the dependency property later, for operations such as setting its value programmatically or obtaining metadata.

示例

下面的示例将注册一个依赖项属性,包括验证回调(不显示回调定义; 有关回调定义的详细信息,请参阅 ValidateValueCallback)。The following example registers a dependency property, including a validation callback (the callback definition is not shown; for details on the callback definition, see ValidateValueCallback).

public static readonly DependencyProperty CurrentReadingProperty = DependencyProperty.Register(
    "CurrentReading",
    typeof(double),
    typeof(Gauge),
    new FrameworkPropertyMetadata(
        Double.NaN,
        FrameworkPropertyMetadataOptions.AffectsMeasure,
        new PropertyChangedCallback(OnCurrentReadingChanged),
        new CoerceValueCallback(CoerceCurrentReading)
    ),
    new ValidateValueCallback(IsValidReading)
);
public double CurrentReading
{
  get { return (double)GetValue(CurrentReadingProperty); }
  set { SetValue(CurrentReadingProperty, value); }
}
Public Shared ReadOnly CurrentReadingProperty As DependencyProperty =
    DependencyProperty.Register("CurrentReading",
        GetType(Double), GetType(Gauge),
        New FrameworkPropertyMetadata(Double.NaN,
            FrameworkPropertyMetadataOptions.AffectsMeasure,
            New PropertyChangedCallback(AddressOf OnCurrentReadingChanged),
            New CoerceValueCallback(AddressOf CoerceCurrentReading)),
        New ValidateValueCallback(AddressOf IsValidReading))

Public Property CurrentReading() As Double
    Get
        Return CDbl(GetValue(CurrentReadingProperty))
    End Get
    Set(ByVal value As Double)
        SetValue(CurrentReadingProperty, value)
    End Set
End Property

注解

有关依赖属性注册的详细信息,请参阅 DependencyPropertyFor more information on dependency property registration, see DependencyProperty.

另请参阅

适用于