Partilhar via


Como: Register an Attached Property

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) and code. Attached properties are a syntax concept defined by Extensible Application Markup Language (XAML). Most attached properties for WPF types are also implemented as dependency properties. Você pode usar Propriedades de dependência em qualquer DependencyObject tipos.

Exemplo

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. In this example, the default value of the IsBubbleSource property is set to false.

A classe do provedor para uma propriedade anexada (mesmo que ele não está registrado como uma propriedade de dependência) deve fornecer get estático e acessadores set que seguem a convenção de nomenclatura Set[AttachedPropertyName] e Get[AttachedPropertyName]. Esses acessadores são necessários para que a agir XAML leitor possa reconhecer a propriedade como um atributo no XAML e resolver tipos apropriados. 

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
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);
}

Consulte também

Referência

DependencyProperty

Conceitos

Visão geral sobre propriedades de dependência

Propriedades de Dependência Personalizada

Outros recursos

Tópicos de Como Fazer sobre Propriedades