下面是依赖对像类的实现:(注,这里涉及到INotifyPropertyChanged接口,大家可以参考MSDN文档了解).
/// <summary>@H_403_3@ /// 依赖对像,主要提供属性值和属性绑定的管理。@H_403_3@ /// </summary>@H_403_3@ public class MyDependencyObject@H_403_3@ {@H_403_3@ private IDictionary<MyDependencyProperty,object> _dict = new Dictionary<MyDependencyProperty,object>();@H_403_3@ private IDictionary<MyDependencyProperty,MyBinding> _bindings = new Dictionary<MyDependencyProperty,MyBinding>(); @H_403_3@ public void SetValue(MyDependencyProperty p,object val)@H_403_3@ {@H_403_3@ @H_403_3@ //如果设置值是默认值,则可不保存,以节省空间.@H_403_3@ object theOldValue = null;@H_403_3@ if (_dict.ContainsKey(p))@H_403_3@ {@H_403_3@ theOldValue = _dict[p];@H_403_3@ //如果已有设置值,且等于当前设置值,则退出@H_403_3@ if (theOldValue == val)@H_403_3@ {@H_403_3@ return;@H_403_3@ }@H_403_3@ //如果设置值等于默认值,则删除已有的设置值。@H_403_3@ if (p.DefaultValue == val)@H_403_3@ {@H_403_3@ _dict.Remove(p);@H_403_3@ return;@H_403_3@ }@H_403_3@ //设置新的字典值@H_403_3@ _dict[p] = val;@H_403_3@ }@H_403_3@ else@H_403_3@ {@H_403_3@ //如果设置值不等于默认值,则增加设置值,否则不做任何设置。@H_403_3@ if (p.DefaultValue != val)@H_403_3@ {@H_403_3@ _dict.Add(p,val);@H_403_3@ }@H_403_3@ else@H_403_3@ {@H_403_3@ return;@H_403_3@ }@H_403_3@ @H_403_3@ }@H_403_3@ @H_403_3@ if (p.PropertyMetadata != null && p.PropertyMetadata.PropertyChangedCallback != null)@H_403_3@ {@H_403_3@ MyDependencyPropertyChangedEventArgs theArgs =@H_403_3@ new MyDependencyPropertyChangedEventArgs(val,theOldValue,p);@H_403_3@ p.PropertyMetadata.PropertyChangedCallback(this,theArgs);@H_403_3@ }@H_403_3@ //如果是双向绑定,则需要同步数据到绑定数据源,这里假设需要双向绑定.@H_403_3@ if (_bindings.ContainsKey(p) == true)@H_403_3@ {@H_403_3@ MyBinding theBinding = _bindings[p];@H_403_3@ if (theBinding.TargetObject != null && theBinding.PropertyName != "")@H_403_3@ {@H_403_3@ System.Reflection.PropertyInfo thePI = theBinding.TargetObject.GetType().GetProperty(theBinding.PropertyName);@H_403_3@ if (thePI != null && thePI.CanWrite==true)@H_403_3@ {@H_403_3@ //对于有索引的设置值比较复杂一点,可利用反射来进行,这里只是演示简单属性。@H_403_3@ //注意,如果目标类实现了INotifyPropertyChanged接口,并有修改触发机制,那么这里的设置@H_403_3@ //会触发目标属性改变事件,就会触发MyDependencyObject_PropertyChanged执行,@H_403_3@ //而MyDependencyObject_PropertyChanged里又调用了SetValue函数,这就会死循环,这也是@H_403_3@ //为什么前面的代码中为什么要判断如果已经有的设置值等于当前设置新值直接退出的缘故,就是@H_403_3@ //为了阻止死循环.当然,在目标属性中set里面做判断也可以,但这里一定要做,@H_403_3@ //原因大家可以自己想一下。@H_403_3@ thePI.SetValue(theBinding.TargetObject,val,null);@H_403_3@ }@H_403_3@ }@H_403_3@ }@H_403_3@ }
public object GetValue(MyDependencyProperty p)@H_403_3@ {@H_403_3@ //如果被动画控制,返回动画计算值。(可能会用到p.Name)
//如果有本地值,返回本地值@H_403_3@ if (_dict.ContainsKey(p))@H_403_3@ {@H_403_3@ return _dict[p];@H_403_3@ }
//如果有Style,则返回Style的值
//返回从可视化树中继承的值
//最后,返回依赖属性的DefaultValue@H_403_3@ return p.DefaultValue;@H_403_3@ }@H_403_3@ /// <summary>@H_403_3@ /// 设置绑定属性,一样是模拟微软的干活,只不过微软的这个方法不是在依赖对象里实现的,@H_403_3@ /// 而是在UIElement里实现的.@H_403_3@ /// </summary>@H_403_3@ /// <param name="p"></param>@H_403_3@ /// <param name="Binding"></param>@H_403_3@ public void SetBinding(MyDependencyProperty p,MyBinding Binding)@H_403_3@ {@H_403_3@ MyBinding theOld = null;@H_403_3@ //需要先将老的绑定找到并记录,因为需要解除挂接.@H_403_3@ if (_bindings.ContainsKey(p))@H_403_3@ {@H_403_3@ theOld = _bindings[p];@H_403_3@ _bindings[p] = Binding;@H_403_3@ }@H_403_3@ else@H_403_3@ {@H_403_3@ _bindings.Add(p,Binding);@H_403_3@ }@H_403_3@ //删除旧的绑定.@H_403_3@ if (theOld != null)@H_403_3@ {@H_403_3@ if (theOld.TargetObject is INotifyPropertyChanged)@H_403_3@ {@H_403_3@ ((INotifyPropertyChanged)theOld.TargetObject).PropertyChanged -= new PropertyChangedEventHandler(MyDependencyObject_PropertyChanged);@H_403_3@ }@H_403_3@ }
//如果是单向绑定或者双向绑定则需要以下挂接。如果只是Onetime则不必要.@H_403_3@ if (Binding.TargetObject is INotifyPropertyChanged)@H_403_3@ {@H_403_3@ ((INotifyPropertyChanged)Binding.TargetObject).PropertyChanged += new PropertyChangedEventHandler(MyDependencyObject_PropertyChanged);@H_403_3@ }
}@H_403_3@ /// <summary>@H_403_3@ /// 目标属性发生变化时的处理事件方法。@H_403_3@ /// </summary>@H_403_3@ /// <param name="sender"></param>@H_403_3@ /// <param name="e"></param>@H_403_3@ void MyDependencyObject_PropertyChanged(object sender,PropertyChangedEventArgs e)@H_403_3@ {@H_403_3@ MyDependencyProperty p = null;@H_403_3@ //找到绑定属性所在的依赖属性@H_403_3@ foreach (var b in _bindings)@H_403_3@ {@H_403_3@ if (b.Value.PropertyName == e.PropertyName)@H_403_3@ {@H_403_3@ p = b.Key;@H_403_3@ break;@H_403_3@ }@H_403_3@ }@H_403_3@ //不为空则处理.@H_403_3@ if (p != null)@H_403_3@ {@H_403_3@ System.Reflection.PropertyInfo thePI = sender.GetType().GetProperty(e.PropertyName);@H_403_3@ if (thePI != null && thePI.CanRead == true)@H_403_3@ {@H_403_3@ object theVal = thePI.GetValue(sender,null);@H_403_3@ SetValue(p,theVal);@H_403_3@ //如果目标类INotifyPropertyChanged,绑定模式是ontime,则下面的代码就是要接触与目标属性的挂接.@H_403_3@ if (sender is INotifyPropertyChanged)@H_403_3@ {@H_403_3@ ((INotifyPropertyChanged)sender).PropertyChanged += new PropertyChangedEventHandler(MyDependencyObject_PropertyChanged);@H_403_3@ }@H_403_3@ }@H_403_3@ }@H_403_3@ }@H_403_3@ }
为了实现依赖属性和绑定属性之间的连动,SetBinding方法至关重要,大家仔细看.
@H_403_3@原文链接: http://www.jb51.cc/article/p-ylcgneae-bcq.html