Silverlight 自定义的附加属性

Silverlight 自定义的附加属性  

 Silverlight  对象 都可以增加属性

 

自定义的附加属性

定义附加属性的依赖项属性注册(使用 RegisterAttached 方法)以及 Get PropertyName 和 Set PropertyName 访问器。在该示例中,附加属性名为 IsBubbleSource。因此,访问器的名称必须为 GetIsBubbleSource 和 SetIsBubbleSource。

例如:

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
"IsBubbleSource",
typeof(Boolean),
typeof(AquariumObject),
new PropertyMetadata(false)
);
public static void SetIsBubbleSource(UIElement element, Boolean value)
{
element.SetValue(IsBubbleSourceProperty, value);
}
public static Boolean GetIsBubbleSource(UIElement element)
{
return (Boolean)element.GetValue(IsBubbleSourceProperty);
}


注意要用 RegisterAttached  不是 Register

<Image aqua:Aquarium.IsBubbleSource="true" .../>  
Image 对象就多了一个IsBubbleSource属性了
 
参看微软文档:
http://msdn.microsoft.com/zh-cn/library/cc903943(v=VS.95).aspx


原文地址:https://www.cnblogs.com/piaocz/p/2220765.html