DependencyProperty.RegisterAttached 方法

定義

向屬性系統註冊附加的屬性。

多載

RegisterAttached(String, Type, Type)

使用指定的屬性名稱、屬性類型和擁有者類型以註冊附加屬性。

RegisterAttached(String, Type, Type, PropertyMetadata)

請使用指定的屬性名稱、屬性類型、擁有者類型和屬性中繼資料登錄附加的屬性。

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

使用指定的屬性類型、擁有者類型、屬性中繼資料以及屬性的值驗證回呼,註冊附加屬性。

RegisterAttached(String, Type, 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

要註冊之相依性屬性的名稱。

propertyType
Type

屬性的類型。

ownerType
Type

正在註冊相依性屬性的擁有者類型。

傳回

DependencyProperty

應該用來設定類別之 public static readonly 欄位值的相依性屬性的識別項。 若為以程式設計方式設定其值或取得中繼資料等的作業,之後就會用識別項參考相依性屬性。

範例

下列範例會使用此 RegisterAttached 簽章在抽象類別上註冊附加屬性。

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) 所定義的屬性概念。 WPF 會將附加屬性實作為相依性屬性。 因為 WPF 附加屬性是相依性屬性,所以可以套用中繼資料,讓一般屬性系統可用於報表配置特性等作業。 如需詳細資訊,請參閱附加屬性概觀

如需相依性屬性註冊的詳細資訊,請參閱 DependencyProperty

另請參閱

適用於

RegisterAttached(String, Type, Type, PropertyMetadata)

請使用指定的屬性名稱、屬性類型、擁有者類型和屬性中繼資料登錄附加的屬性。

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

要註冊之相依性屬性的名稱。

propertyType
Type

屬性的類型。

ownerType
Type

正在註冊相依性屬性的擁有者類型。

defaultMetadata
PropertyMetadata

相依性屬性的屬性中繼資料。 這可以包含預設值及其他特性。

傳回

DependencyProperty

應該用來設定類別之 public static readonly 欄位值的相依性屬性的識別項。 若為以程式設計方式設定其值或取得中繼資料等的作業,之後就會用識別項參考相依性屬性。

備註

附加屬性是由 Extensible Application Markup Language (XAML) 所定義的屬性概念。 WPF 會將附加屬性實作為相依性屬性。 因為 WPF 附加屬性是相依性屬性,所以可以套用中繼資料,讓一般屬性系統可用於報表配置特性等作業。 如需詳細資訊,請參閱附加屬性概觀

如需相依性屬性註冊的詳細資訊,請參閱 DependencyProperty

針對值繼承相依性屬性使用 RegisterAttached

向 註冊相依性屬性 RegisterAttached Register 而非 的其中一個特定案例是支援屬性值繼承。 即使 類別定義公開相依性屬性的屬性包裝函式存取子,而且即使您不想要公開 Get* 和 Set* 靜態方法,也應該向 註冊值繼承相依性屬性 RegisterAttached ,以提供真正的附加屬性支援存取子。 雖然屬性值繼承可能適用于非附加的相依性屬性,但未定義執行時間樹狀結構中特定元素界限的非附加屬性繼承行為。 將屬性註冊為附加實際上會使附加屬性成為屬性系統的全域屬性,並確保屬性值繼承可跨專案樹狀結構中的所有界限運作。 一律使用 RegisterAttached 來註冊您在 Inherits 中繼資料中指定的屬性。 如需詳細資訊,請參閱屬性值繼承

另請參閱

適用於

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

使用指定的屬性類型、擁有者類型、屬性中繼資料以及屬性的值驗證回呼,註冊附加屬性。

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
Public Shared Function RegisterAttached (name As String, propertyType As Type, ownerType As Type, defaultMetadata As PropertyMetadata, validateValueCallback As ValidateValueCallback) As DependencyProperty

參數

name
String

要註冊之相依性屬性的名稱。

propertyType
Type

屬性的類型。

ownerType
Type

正在註冊相依性屬性的擁有者類型。

defaultMetadata
PropertyMetadata

相依性屬性的屬性中繼資料。 這可以包含預設值及其他特性。

validateValueCallback
ValidateValueCallback

回呼的參考,應該執行對相依性屬性值之一般類型驗證以外的任何自訂驗證。

傳回

DependencyProperty

應該用來設定類別之 public static readonly 欄位值的相依性屬性的識別項。 若為以程式設計方式設定其值或取得中繼資料等的作業,之後就會用識別項參考相依性屬性。

範例

下列範例會使用此 RegisterAttached 簽章在抽象類別上註冊附加屬性。 這個附加屬性是列舉類型屬性,而註冊會新增驗證回呼,以確認所提供的值是列舉的值。

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) 所定義的屬性概念。 WPF 會將附加屬性實作為相依性屬性。 因為 WPF 附加屬性是相依性屬性,所以可以套用中繼資料,讓一般屬性系統可用於報表配置特性等作業。 如需詳細資訊,請參閱附加屬性概觀

如需相依性屬性註冊的詳細資訊,請參閱 DependencyProperty

針對值繼承相依性屬性使用 RegisterAttached

向 註冊相依性屬性 RegisterAttached Register 而非 的其中一個特定案例是支援屬性值繼承。 即使 類別定義公開相依性屬性的屬性包裝函式存取子,而且即使您不想要公開 Get* 和 Set* 靜態方法,也應該向 註冊值繼承相依性屬性 RegisterAttached ,以提供真正的附加屬性支援存取子。 雖然屬性值繼承可能適用于非附加的相依性屬性,但未定義執行時間樹狀結構中特定元素界限的非附加屬性繼承行為。 將屬性註冊為附加實際上會使附加屬性成為屬性系統的全域屬性,並確保屬性值繼承可跨專案樹狀結構中的所有界限運作。 一律使用 RegisterAttached 來註冊您在 Inherits 中繼資料中指定的屬性。 如需詳細資訊,請參閱屬性值繼承

另請參閱

適用於