DependencyProperty.RegisterAttached 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用属性的指定属性名称、属性类型、所有者类型和属性元数据注册附加的依赖属性。
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
要注册的依赖属性的名称。
- 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);
}
}