DependencyProperty.RegisterAttached DependencyProperty.RegisterAttached DependencyProperty.RegisterAttached DependencyProperty.RegisterAttached Method

定義

向屬性系統註冊附加的屬性。Registers an attached property with the property system.

多載

RegisterAttached(String, Type, Type) RegisterAttached(String, Type, Type) RegisterAttached(String, Type, Type) RegisterAttached(String, Type, Type)

使用指定的屬性名稱、屬性類型和擁有者類型以註冊附加屬性。Registers an attached property with the specified property name, property type, and owner type.

RegisterAttached(String, Type, Type, PropertyMetadata) RegisterAttached(String, Type, Type, PropertyMetadata) RegisterAttached(String, Type, Type, PropertyMetadata) RegisterAttached(String, Type, Type, PropertyMetadata)

請使用指定的屬性名稱、屬性類型、擁有者類型和屬性中繼資料登錄附加的屬性。Registers an attached property with the specified property name, property type, owner type, and property metadata.

RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback) RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback) RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的屬性類型、擁有者類型、屬性中繼資料以及屬性的值驗證回呼,註冊附加屬性。Registers an attached property with the specified property type, owner type, property metadata, and value validation callback for the property.

RegisterAttached(String, Type, Type) RegisterAttached(String, Type, Type) RegisterAttached(String, Type, Type) RegisterAttached(String, Type, Type)

使用指定的屬性名稱、屬性類型和擁有者類型以註冊附加屬性。Registers an attached property with the specified property name, property type, and owner type.

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

參數

name
String String String String

要註冊之相依性屬性的名稱。The name of the dependency property to register.

propertyType
Type Type Type Type

屬性的類型。The type of the property.

ownerType
Type Type Type 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.

範例

下列範例會使用此RegisterAttached簽章, 在抽象類別上註冊附加屬性。The following example registers an attached property on an abstract class using this RegisterAttached signature.

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
  "IsBubbleSource",
  typeof(Boolean),
  typeof(AquariumObject2)
);
public static void SetIsBubbleSource(UIElement element, Boolean value)
{
    element.SetValue(IsBubbleSourceProperty, value);
}
public static Boolean GetIsBubbleSource(UIElement element)
{
    return (Boolean)element.GetValue(IsBubbleSourceProperty);
}
Public Shared ReadOnly IsBubbleSourceProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsBubbleSource", GetType(Boolean), GetType(AquariumObject2))
Public Shared Sub SetIsBubbleSource(ByVal element As UIElement, ByVal value As Boolean)
    element.SetValue(IsBubbleSourceProperty, value)
End Sub
Public Shared Function GetIsBubbleSource(ByVal element As UIElement) As Boolean
    Return CType(element.GetValue(IsBubbleSourceProperty), Boolean)
End Function

備註

附加屬性是由Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML)定義的屬性概念。An attached property is a property concept defined by Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML). WPFWPF將附加屬性實作為相依性屬性。implements attached properties as dependency properties. WPFWPF因為附加屬性是相依性屬性, 所以可以套用中繼資料, 供一般屬性系統用來進行作業, 例如報告版面配置特性。Because the WPFWPF attached properties are dependency properties, they can have metadata applied that can be used by the general property system for operations such as reporting layout characteristics. 如需詳細資訊,請參閱附加屬性概觀For more information, see Attached Properties Overview.

如需相依性屬性註冊的詳細資訊DependencyProperty, 請參閱。For more information on dependency property registration, see DependencyProperty.

另請參閱

RegisterAttached(String, Type, Type, PropertyMetadata) RegisterAttached(String, Type, Type, PropertyMetadata) RegisterAttached(String, Type, Type, PropertyMetadata) RegisterAttached(String, Type, Type, PropertyMetadata)

請使用指定的屬性名稱、屬性類型、擁有者類型和屬性中繼資料登錄附加的屬性。Registers an attached property with the specified property name, property type, owner type, and property metadata.

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

參數

name
String String String String

要註冊之相依性屬性的名稱。The name of the dependency property to register.

propertyType
Type Type Type Type

屬性的類型。The type of the property.

ownerType
Type Type Type Type

正在註冊相依性屬性的擁有者類型。The owner type that is registering the dependency property.

defaultMetadata
PropertyMetadata PropertyMetadata PropertyMetadata PropertyMetadata

相依性屬性的屬性中繼資料。Property metadata for the dependency property. 這可以包含預設值及其他特性。This can include the default value as well as other characteristics.

傳回

應該用來設定類別之 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.

備註

附加屬性是由Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML)定義的屬性概念。An attached property is a property concept defined by Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML). WPFWPF將附加屬性實作為相依性屬性。implements attached properties as dependency properties. WPFWPF因為附加屬性是相依性屬性, 所以可以套用中繼資料, 供一般屬性系統用來進行作業, 例如報告版面配置特性。Because the WPFWPF attached properties are dependency properties, they can have metadata applied that can be used by the general property system for operations such as reporting layout characteristics. 如需詳細資訊,請參閱附加屬性概觀For more information, see Attached Properties Overview.

如需相依性屬性註冊的詳細資訊DependencyProperty, 請參閱。For more information on dependency property registration, see DependencyProperty.

將 System.windows.dependencyproperty.registerattached 用於值繼承相依性屬性Use RegisterAttached for Value-inheriting Dependency Properties

向註冊相依性屬性RegisterAttached而不是的Register一個特定案例是支援屬性值繼承。One particular scenario for registering a dependency property with RegisterAttached instead of Register is to support property value inheritance. RegisterAttached即使類別定義了公開相依性屬性的屬性包裝函式, 甚至您不想要公開 Get * 和 Set * 靜態方法來提供真正的附加, 您也應該註冊值繼承的相依性屬性。屬性支援存取子。You should register value-inheriting dependency properties with RegisterAttached even if the class defines property wrapper accessors that expose the dependency property, and even if you do not intend to expose Get* and Set* static methods to provide true attached property support accessors. 雖然屬性值繼承可能會因為未附加的相依性屬性而出現, 但未附加的屬性會透過執行時間樹狀結構中的特定專案界限進行的繼承行為是未定義的。Although property value inheritance might appear to work for nonattached dependency properties, the inheritance behavior for a nonattached property through certain element boundaries in the runtime tree is undefined. 將屬性註冊為附加, 會將附加屬性設為屬性系統的全域屬性, 並確保屬性值繼承適用于元素樹狀結構中的所有界限。Registering the property as attached effectively makes the attached property a global property to the property system, and assures that property value inheritance works across all boundaries in an element tree. 請一律RegisterAttached使用來註冊您在元Inherits資料中指定的屬性。Always use RegisterAttached to register properties where you specify Inherits in the metadata. 如需詳細資訊,請參閱屬性值繼承For more information, see Property Value Inheritance.

另請參閱

RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback) RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback) RegisterAttached(String, Type, Type, PropertyMetadata, ValidateValueCallback)

使用指定的屬性類型、擁有者類型、屬性中繼資料以及屬性的值驗證回呼,註冊附加屬性。Registers an attached property with the specified property type, owner type, property metadata, and value validation callback for the property.

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

參數

name
String String String String

要註冊之相依性屬性的名稱。The name of the dependency property to register.

propertyType
Type Type Type Type

屬性的類型。The type of the property.

ownerType
Type Type Type Type

正在註冊相依性屬性的擁有者類型。The owner type that is registering the dependency property.

defaultMetadata
PropertyMetadata PropertyMetadata PropertyMetadata PropertyMetadata

相依性屬性的屬性中繼資料。Property metadata for the dependency property. 這可以包含預設值及其他特性。This can include the default value as well as other characteristics.

validateValueCallback
ValidateValueCallback ValidateValueCallback 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.

範例

下列範例會使用此RegisterAttached簽章, 在抽象類別上註冊附加屬性。The following example registers an attached property on an abstract class using this RegisterAttached signature. 這個附加屬性是列舉型別屬性, 而且註冊會加入驗證回呼, 以確認提供的值是列舉的值。This attached property is an enumeration type property, and the registration adds a validation callback to verify that the provided value is a value of the enumeration.

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
  "IsBubbleSource",
  typeof(Boolean),
  typeof(AquariumObject2)
);
public static void SetIsBubbleSource(UIElement element, Boolean value)
{
    element.SetValue(IsBubbleSourceProperty, value);
}
public static Boolean GetIsBubbleSource(UIElement element)
{
    return (Boolean)element.GetValue(IsBubbleSourceProperty);
}
Public Shared ReadOnly IsBubbleSourceProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsBubbleSource", GetType(Boolean), GetType(AquariumObject2))
Public Shared Sub SetIsBubbleSource(ByVal element As UIElement, ByVal value As Boolean)
    element.SetValue(IsBubbleSourceProperty, value)
End Sub
Public Shared Function GetIsBubbleSource(ByVal element As UIElement) As Boolean
    Return CType(element.GetValue(IsBubbleSourceProperty), Boolean)
End Function

備註

附加屬性是由Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML)定義的屬性概念。An attached property is a property concept defined by Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML). WPFWPF將附加屬性實作為相依性屬性。implements attached properties as dependency properties. WPFWPF因為附加屬性是相依性屬性, 所以可以套用中繼資料, 供一般屬性系統用來進行作業, 例如報告版面配置特性。Because the WPFWPF attached properties are dependency properties, they can have metadata applied that can be used by the general property system for operations such as reporting layout characteristics. 如需詳細資訊,請參閱附加屬性概觀For more information, see Attached Properties Overview.

如需相依性屬性註冊的詳細資訊DependencyProperty, 請參閱。For more information on dependency property registration, see DependencyProperty.

將 System.windows.dependencyproperty.registerattached 用於值繼承相依性屬性Use RegisterAttached for Value-inheriting Dependency Properties

向註冊相依性屬性RegisterAttached而不是的Register一個特定案例是支援屬性值繼承。One particular scenario for registering a dependency property with RegisterAttached instead of Register is to support property value inheritance. RegisterAttached即使類別定義了公開相依性屬性的屬性包裝函式, 甚至您不想要公開 Get * 和 Set * 靜態方法來提供真正的附加, 您也應該註冊值繼承的相依性屬性。屬性支援存取子。You should register value-inheriting dependency properties with RegisterAttached even if the class defines property wrapper accessors that expose the dependency property, and even if you do not intend to expose Get* and Set* static methods to provide true attached property support accessors. 雖然屬性值繼承可能會因為未附加的相依性屬性而出現, 但未附加的屬性會透過執行時間樹狀結構中的特定專案界限進行的繼承行為是未定義的。Although property value inheritance might appear to work for nonattached dependency properties, the inheritance behavior for a nonattached property through certain element boundaries in the runtime tree is undefined. 將屬性註冊為附加, 會將附加屬性設為屬性系統的全域屬性, 並確保屬性值繼承適用于元素樹狀結構中的所有界限。Registering the property as attached effectively makes the attached property a global property to the property system, and assures that property value inheritance works across all boundaries in an element tree. 請一律RegisterAttached使用來註冊您在元Inherits資料中指定的屬性。Always use RegisterAttached to register properties where you specify Inherits in the metadata. 如需詳細資訊,請參閱屬性值繼承For more information, see Property Value Inheritance.

另請參閱

適用於