注册方法
我们知道注册依赖属性使用Register方法,注册附加属性则使用RegisterAttached方法,如下代码
public class DPCustomPeople:DependencyObject { public static readonly DependencyProperty AgeProperty = DependencyProperty.Register("Age", typeof(int), typeof(DPCustomPeople)); public static readonly DependencyProperty Age2Property = DependencyProperty.RegisterAttached("Age2", typeof(int), typeof(DPCustomPeople)); }
包装属性
public int Age { get { return (int)GetValue(AgeProperty); } set { SetValue(AgeProperty, value); } } public int Age2 { get { return (int)GetValue(Age2Property); } set { SetValue(Age2Property, value); } }
一般默认依赖属性使用CLR属性进行包装,附加属性使用Get,Set方法进行包装.
下面我则均以属性进行包装,从表面上看两者除了方法不同,其他都是一样的
那么附加属性的魔力到底何在呢?
XAML的魔力
public class AttachEntity { public static double GetWidth(DependencyObject obj) { return (double)obj.GetValue(Button.WidthProperty); } public static void SetWidth(DependencyObject obj, double value) { obj.SetValue(Button.WidthProperty, value); } }考虑以上代码,你可能从来都没有试过在没有附加属性的情况下,下面的只有Get,Set的方法,然后尝试在XAML中设置
<Button local:AttachEntity.Width="300" Content="Button" />
即来看到这里,我们不妨试试依赖属性
public static readonly DependencyProperty AgeProperty = DependencyProperty.Register("Age", typeof(int), typeof(DPCustomPeople), new UIPropertyMetadata(0,(sender,args)=> { var element = sender as DPCustomPeople; })); public static int GetAge(DependencyObject obj) { return (int)obj.GetValue(AgeProperty); } public static void SetAge(DependencyObject obj, int value) { obj.SetValue(AgeProperty, value); }
<Button local:DPCustomPeople.Age="2" Content="Button" Name="button1"/>
程序运行正常,发生下列情况
- 可以取到附加属性值
- 属性变更通知没有发生
默认属性元数据
现在是时候来看看依赖属性与附加属性的区别了,以下是内部注册方法
第一段:注册依赖属性的方法
public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata, ValidateValueCallback validateValueCallback) { RegisterParameterValidation(name, propertyType, ownerType); PropertyMetadata defaultMetadata = null; if ((typeMetadata != null) && typeMetadata.DefaultValueWasSet()) { defaultMetadata = new PropertyMetadata(typeMetadata.DefaultValue); } DependencyProperty property = RegisterCommon(name, propertyType, ownerType, defaultMetadata, validateValueCallback); if (typeMetadata != null) { property.OverrideMetadata(ownerType, typeMetadata); } return property; }
第二段:注册附加属性方法
public static DependencyProperty RegisterAttached(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback) { RegisterParameterValidation(name, propertyType, ownerType); return RegisterCommon(name, propertyType, ownerType, defaultMetadata, validateValueCallback); }
请先忽略上面划线部分的代码,现在我们看到在注册依赖属性时,前后多了一些处理.
原来是属性元数据在作怪。
注册依赖属性时,会传入一个属性元数据,但内部定义了一个默认的属性元数据(defaultMetadata ),当依赖属性注册完毕后,则重写了属性元数据(OverrideMetadata),而注册附加属性时,则直接传入参数.这个参数则直接作为了依赖属性的默认元数据,如下代码
var people = new DPCustomPeople(); var defaultMetadata=DPCustomPeople.AgeProperty.DefaultMetadata; var metadata = DPCustomPeople.AgeProperty.GetMetadata(people);
附加属性只有默认属性元数据,根据以上源码,我们甚至可以改造附加属性为依赖属性,下面的附加属性则变成了依赖属性
注意点:
(1)重写属性元数据是一个合并的过程,所以重写的变更事件并不会触发
(2)若属性元数据已经注册完毕,同个类型的属性元数据不可重复重写
public static readonly DependencyProperty Age3Property; static DPCustomPeople() { var metaData = new PropertyMetadata(0, new PropertyChangedCallback((sender, args) => { //not occur MessageBox.Show("hello"); })); PropertyMetadata defaultMetadata = null; if ((metaData != null)) { defaultMetadata = new PropertyMetadata(metaData.DefaultValue, new PropertyChangedCallback((sender, args) => { MessageBox.Show("hello"); })); } Age3Property = DependencyProperty.RegisterAttached("Age3", typeof(int), typeof(DPCustomPeople), defaultMetadata); Age3Property.OverrideMetadata(typeof(DPCustomPeople), metaData); }
经过上面的推敲,我们可以看到依赖属性与附加属性的区别在于属性元数据的变化,附加属性也变的不再那么神奇了.下面欢迎你加入讨论中来.
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-665049/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12639172/viewspace-665049/