语言

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

属性的类型,作为类型引用。

ownerType
TypeName Type

将依赖属性注册为类型引用的所有者类型。

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

适用于

另请参阅