DependencyProperty.RegisterAttached 方法

定义

使用指定的属性名称、属性类型、所有者类型和属性元数据注册附加依赖属性。

 static DependencyProperty RegisterAttached(winrt::hstring const& name, TypeName const& propertyType, TypeName const& ownerType, PropertyMetadata const& defaultMetadata);
public static DependencyProperty RegisterAttached(string name, System.Type propertyType, System.Type ownerType, PropertyMetadata defaultMetadata);
function registerAttached(name, propertyType, ownerType, defaultMetadata)
Public Shared Function RegisterAttached (name As String, propertyType As Type, ownerType As Type, defaultMetadata As PropertyMetadata) As DependencyProperty

参数

name
String

winrt::hstring

要注册的依赖属性的名称。

propertyType
TypeName Type

属性的类型,作为类型引用 (System.Type for Microsoft .NET,Visual C++ 组件扩展的 TypeName 帮助程序结构 (C++/CX) ) 。

ownerType
TypeName Type

注册依赖属性的所有者类型,作为类型引用 (System.Type for Microsoft .NET,Visual C++ 组件扩展的 TypeName 帮助程序结构 (C++/CX) ) 。

defaultMetadata
PropertyMetadata

属性元数据实例。 这可以包含 PropertyChangedCallback 实现参考。

返回

一个依赖项属性标识符,应用于设置类中公共静态只读字段的值。 然后,该标识符稍后将用于引用附加属性,用于以编程方式设置其值或附加 绑定等操作。

示例

此示例定义派生自 DependencyObject 的类,并定义附加属性以及标识符字段。 此类的方案是,它是一个服务类,它声明其他 UI 元素可以在 XAML 中设置的附加属性,并且该服务可能在运行时对这些 UI 元素的附加属性值执行操作。 有关更多示例,请参阅 自定义附加属性

public abstract class AquariumServices : DependencyObject
{
    public enum Buoyancy {Floats,Sinks,Drifts}

    public static readonly DependencyProperty BuoyancyProperty = DependencyProperty.RegisterAttached(
      "Buoyancy",
      typeof(Buoyancy),
      typeof(AquariumServices),
      new PropertyMetadata(Buoyancy.Floats)
    );
    public static void SetBuoyancy(DependencyObject element, Buoyancy value)
    {
        element.SetValue(BuoyancyProperty, value);
    }
    public static Buoyancy GetBuoyancy(DependencyObject element)
    {
        return (Buoyancy)element.GetValue(BuoyancyProperty);
    }
}
Public Class AquariumServices
    Inherits DependencyObject
    Public Enum Buoyancy
        Floats
        Sinks
        Drifts
    End Enum

    Public Shared ReadOnly BuoyancyProperty As DependencyProperty = _
          DependencyProperty.RegisterAttached(
          "Buoyancy", _
          GetType(Buoyancy), _
          GetType(AquariumServices), _
          New PropertyMetadata(Buoyancy.Floats))


    Public Sub SetBuoyancy(element As DependencyObject, value As Buoyancy)
        element.SetValue(BuoyancyProperty, value)
    End Sub
    Public Function GetBuoyancy(element As DependencyObject) As Buoyancy
        GetBuoyancy = CType(element.GetValue(BuoyancyProperty), Buoyancy)
    End Function
End Class

适用于

另请参阅