HOW TO:註冊附加屬性How to: Register an Attached Property

此範例示範如何註冊附加屬性,以及提供公用存取子,讓您可以透過 Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) 和程式碼使用此屬性。This example shows how to register an attached property and provide public accessors so that you can use the property in both Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) and code. 附加屬性是透過 Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) 所定義的語法概念。Attached properties are a syntax concept defined by Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML). WPFWPF 類型的大部分附加屬性也會以相依性屬性的方式實作。Most attached properties for WPFWPF types are also implemented as dependency properties. 您可以使用相依性屬性上任何DependencyObject型別。You can use dependency properties on any DependencyObject types.

範例Example

下列範例示範如何使用附加的屬性註冊為相依性屬性,RegisterAttached方法。The following example shows how to register an attached property as a dependency property, by using the RegisterAttached method. 提供者類別可以選擇提供在另一個類別上使用屬性時所適用屬性的預設中繼資料,除非該類別會覆寫中繼資料。The provider class has the option of providing default metadata for the property that is applicable when the property is used on another class, unless that class overrides the metadata. 在此範例中,IsBubbleSource 屬性的預設值設定為 falseIn this example, the default value of the IsBubbleSource property is set to false.

附加屬性的提供者類別 (即使未註冊為相依性屬性也是一樣) 必須提供遵循命名慣例 Set[AttachedPropertyName]Get[AttachedPropertyName] 的靜態 get 和 set 存取子。The provider class for an attached property (even if it is not registered as a dependency property) must provide static get and set accessors that follow the naming convention Set[AttachedPropertyName] and Get[AttachedPropertyName]. 這些存取子是必要的,因此正在處理的 XAMLXAML 讀取器可以將該屬性 (property) 辨識為 XAMLXAML 中的屬性 (attribute),並解析適當的類型。These accessors are required so that the acting XAMLXAML reader can recognize the property as an attribute in XAMLXAML and resolve the appropriate types.

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
  "IsBubbleSource",
  typeof(Boolean),
  typeof(AquariumObject),
  new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)
);
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(AquariumObject), New FrameworkPropertyMetadata(False, FrameworkPropertyMetadataOptions.AffectsRender))
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

另請參閱See also