为Silverlight控件添加自定义属性
昨天学习了自定义控件,今天就学习为控件添加自定义属性,找到了万事大吉老师的博客,很顺利第实现了一遍,转过来。
http://www.cnblogs.com/wsdj-ITtech/archive/2009/07/17/1525416.html 下面我们来以一个MyText属性为例演示如何创建一个自定义属性,这个例子仍将会用到前面的内容。首先我们要为DependencyObject的属性声明一个公有的DependencyProperty,这个属性是静态的,可以用于数据绑定,声明代码:
public static DependencyProperty TextProperty;
静态属性在静态构造函数中初始化,先看代码: static SimpleButton()
{
TextProperty = DependencyProperty.Register("MyText",
typeof(string),
typeof(SimpleButton),
new PropertyMetadata("默认值", new PropertyChangedCallback(SimpleButton.OnTextPropertyChanged)));
}
DependencyProperty应该由DependencyProperty.Register创建,在MSDN中,这个函数的原型如下:{
TextProperty = DependencyProperty.Register("MyText",
typeof(string),
typeof(SimpleButton),
new PropertyMetadata("默认值", new PropertyChangedCallback(SimpleButton.OnTextPropertyChanged)));
}
public static DependencyProperty Register(
string name,
Type propertyType,
Type ownerType,
PropertyMetadata typeMetadata
)
string name,
Type propertyType,
Type ownerType,
PropertyMetadata typeMetadata
)
其中name是属性在XAML中使用的名字,propertyType则是属性的类型,注意这个属性理论上可以是任何类型,但是一般只使用整数、布尔、字符串、还有UI元素这些类型,其它类型需要定义复杂的字符串到对象的属性文法作为转换规则(之后可能会单写一篇blog讲converter)。ownerType就是我们自己定义的类了。typeMetadata有点复杂,PropertyMetadata构造函数有3个重载,提供选择指定defaultValue和propertyChangedCallback中任意一个或者同时指定2个。原型如下:
public PropertyMetadata
(
Object defaultValue,
PropertyChangedCallback propertyChangedCallback
)
(
Object defaultValue,
PropertyChangedCallback propertyChangedCallback
)
defaultValue是属性的默认值没什么可说,propertyChangedCallback也很简单,就是当属性改变时的处理函数,这个是把属性同我们的类联系起来的关键了,PropertyChangedCallback 是个委托类型,它的签名也可以在MSDN查到:
public delegate void PropertyChangedCallback
(
DependencyObject d,
DependencyPropertyChangedEventArgs e
)
(
DependencyObject d,
DependencyPropertyChangedEventArgs e
)
下面是我们对MyText属性的具体实现(这个尤其重要,没这个代码单单看前面的实现不了):

#region 添加自定义控件属性
public static DependencyProperty TextProperty;
#region 此控件的C#属性
public string MyText
{
get
{
return (string)base.GetValue(TextProperty);
}
set
{
base.SetValue(TextProperty, value);
}
}
#endregion
#region 静态构造函数
static MySilverButton()
{
TextProperty = DependencyProperty.Register("MyText",
typeof(string),
typeof(MySilverButton),
new PropertyMetadata("默认属性值", new PropertyChangedCallback(MySilverButton.OnTextPropertyChanged)));
}
#endregion
private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as MySilverButton).OnTextPropertyChanged(e);
}
void OnTextPropertyChanged(DependencyPropertyChangedEventArgs e)
{
try
{
TextBlock BgTextBlock = this.GetTemplateChild("ButtonCaption") as TextBlock;
BgTextBlock.Text = e.NewValue as string;
}
catch (Exception ex)
{
string ks = ex.ToString();
}
}
#endregion
//有些时候,我们希望在模板生效的时候就对某些模板成员进行操作,如绑定事件,
//调整属性等,就需要一个事件OnApplyTemplate,我们只能通过override父类的OnApplyTemplate来响应模板生效
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
//我们在属性MyText变化的时候直接GetTemplateChild,并没有考虑到这时TextBlock是否已经创建,
//在SL中,MyText属性设置其实是发生在Template生效之前的,所以设置MyText的时候取不到ButtonCaption元素。
//那么,我们就需要考虑在加载模板完成的时候,取出MyText属性并且设置到ButtonCaption上
try
{
var tb = GetTemplateChild("ButtonCaption") as TextBlock;
(GetTemplateChild("ButtonCaption") as TextBlock).Text = this.MyText;
}
catch (Exception ex)
{
string ke = ex.ToString();
}
public static DependencyProperty TextProperty;
#region 此控件的C#属性
public string MyText
{
get
{
return (string)base.GetValue(TextProperty);
}
set
{
base.SetValue(TextProperty, value);
}
}
#endregion
#region 静态构造函数
static MySilverButton()
{
TextProperty = DependencyProperty.Register("MyText",
typeof(string),
typeof(MySilverButton),
new PropertyMetadata("默认属性值", new PropertyChangedCallback(MySilverButton.OnTextPropertyChanged)));
}
#endregion
private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as MySilverButton).OnTextPropertyChanged(e);
}
void OnTextPropertyChanged(DependencyPropertyChangedEventArgs e)
{
try
{
TextBlock BgTextBlock = this.GetTemplateChild("ButtonCaption") as TextBlock;
BgTextBlock.Text = e.NewValue as string;
}
catch (Exception ex)
{
string ks = ex.ToString();
}
}
#endregion
//有些时候,我们希望在模板生效的时候就对某些模板成员进行操作,如绑定事件,
//调整属性等,就需要一个事件OnApplyTemplate,我们只能通过override父类的OnApplyTemplate来响应模板生效
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
//我们在属性MyText变化的时候直接GetTemplateChild,并没有考虑到这时TextBlock是否已经创建,
//在SL中,MyText属性设置其实是发生在Template生效之前的,所以设置MyText的时候取不到ButtonCaption元素。
//那么,我们就需要考虑在加载模板完成的时候,取出MyText属性并且设置到ButtonCaption上
try
{
var tb = GetTemplateChild("ButtonCaption") as TextBlock;
(GetTemplateChild("ButtonCaption") as TextBlock).Text = this.MyText;
}
catch (Exception ex)
{
string ke = ex.ToString();
}
接下来,我们回到MySLbutton项目测试我们新加的属性是否生效。修改Page.xaml文件中的MySilverButton控件属性如下:
<custom:MySilverButton x:Name="MyFirstSLbutton" Click="MyFirstSLbutton_Click" MyText="自定义属性值">
</custom:MySilverButton>
</custom:MySilverButton>
其中,MyText就是我们自定义的属性,当我们进行上述修改时,我们可以立刻看到显示效果。当然,我们也可以在Page.xaml.cs中对此属性进行动态赋值,代码如下:
this.MyFirstSLbutton.MyText = "动态赋值给自定义属性";