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.

另請參閱

適用於