//建立绑定,把绑定到的对象存起来,在表达式解析完成后,外面再监听这些对象的值变化 private void SetBinding(object o, string path) { //条件部分不包括 if (path != null) { int index = path.IndexOf("["); if (index != -1) { path = path.Substring(0, index); } } //如果建立过绑定,不重复建立 BindInfo info = new BindInfo() { Object = o, Path = path }; if (!this.binds.Contains(info)) { //把绑定信息存起来,以便检查 binds.Add(info); //建立绑定关系 Binding b = new Binding(); b.Source = o; b.Path = new System.Windows.PropertyPath(path); BindingSlave bs = new BindingSlave(); BindingOperations.SetBinding(bs, BindingSlave.ValueProperty, b); Bindings.Add(bs); } }
/// <summary> /// 当属性发生变化时,发送Value改变事件,以便多绑定可以监听这个属性的变化。 /// </summary> /// <param name="depObj">BindingSlave本身</param> /// <param name="e">属性变化参数</param> private static void OnValueChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e) { BindingSlave slave = depObj as BindingSlave; Log.Debug("绑定slave值变了,值为:" + (slave.Value == null ? "null" : slave.Value)); slave.OnPropertyChanged("Value"); }
//监听依赖属性变化,调用属性变化方法 public static void Watch(this DependencyObject source, string dependencyPropertyName, PropertyChangedCallback callback) { if (dependencyPropertyName == null || source == null || callback == null) throw new ArgumentNullException(); Binding binding = new Binding(dependencyPropertyName) { Source = source, Mode = BindingMode.OneWay }; BindingSlave bs = new BindingSlave(); _bindings.Add(bs); bs.PropertyChanged += (o, e) => { callback(source, new DependencyPropertyChangedEventArgs()); }; BindingOperations.SetBinding(bs, BindingSlave.ValueProperty, binding); }
//监听集合本身的变化过程 private void MonityCollection(BindingSlave bs) { //开始要监听集合中对象的属性变化过程 foreach (object obj in (IEnumerable)bs.Value) { if (obj is GeneralObject) { (obj as GeneralObject).DynamicPropertyChanged += PropertyChangedHandler; } } //监听集合本身的变化过程 (bs.Value as INotifyCollectionChanged).CollectionChanged += (o1, e1) => { Assign(bs.Result); //reset时,监听集合自身所有对象变化 if (e1.Action == NotifyCollectionChangedAction.Reset) { foreach (object obj in (IEnumerable)o1) { if (obj is GeneralObject) { (obj as GeneralObject).DynamicPropertyChanged += PropertyChangedHandler; } } } //监听集合中新增对象的属性变化 if (e1.NewItems != null) { foreach (object obj in e1.NewItems) { if (obj is GeneralObject) { (obj as GeneralObject).DynamicPropertyChanged += PropertyChangedHandler; } } } //删除集合中移除对象的属性变化 if (e1.OldItems != null) { foreach (object obj in e1.OldItems) { if (obj is GeneralObject) { (obj as GeneralObject).DynamicPropertyChanged -= PropertyChangedHandler; } } } }; }
//建立绑定,把绑定到的对象存起来,在表达式解析完成后,外面再监听这些对象的值变化 private void SetBinding(object o, string path) { //条件部分不包括 if (path != null) { int index = path.IndexOf("["); if (index != -1) { path = path.Substring(0, index); } } //如果建立过绑定,不重复建立 BindInfo info = new BindInfo() {Object = o, Path = path}; if (!this.binds.Contains(info)) { //把绑定信息存起来,以便检查 binds.Add(info); //建立绑定关系 Binding b = new Binding(); b.Source = o; b.Path = new System.Windows.PropertyPath(path); BindingSlave bs = new BindingSlave(); BindingOperations.SetBinding(bs, BindingSlave.ValueProperty, b); Bindings.Add(bs); } }
public void OnLoaded(object o, RoutedEventArgs e) { //避免重复工作 _targetObject.GetType().GetEvent("Loaded").RemoveEventHandler(_targetObject, handler); handler = null; //对表达式进行解析,在绑定发生变化时,调用表达式执行 //可以对一组表达式工作,表达式之间用'|or|'分割 string[] strs = Str.Split(new String[] { "|or|" }, StringSplitOptions.RemoveEmptyEntries); foreach (string str in strs) { Program prog = new Program(str, _targetObject, true); Delegate result = prog.Parse(prog.Exp); Result = result; Expression = prog.exp; //如果目标是一个代理,直接把编译结果给目标 if (_targetProperty != null && _targetProperty.PropertyType == typeof(Delegate)) { _targetProperty.SetValue(_targetObject, result, null); return; } //如果有事件,则只是在事件发生时才执行表达式。第一次默认及属性变化都不执行。 if (prog.Events.Count == 0) { //先执行一次默认赋值过程,然后当属性值改变时,再调用每个赋值过程 Assign(result); foreach (BindingSlave bs in prog.Bindings) { //把BS保存起来,以免丢失 bs.Result = result; _bindings.Add(bs); bs.PropertyChanged += (o1, e1) => { if (e1.PropertyName == "Value") { BindingSlave _bs = (BindingSlave)o1; Assign(bs.Result); //如果是集合本身变了,要监听集合内容的变化过程 if (_bs.Value is INotifyCollectionChanged) { MonityCollection(_bs); } } }; //如果绑定到了集合,要监听集合变化过程 if (bs.Value is INotifyCollectionChanged) { MonityCollection(bs); } } } //执行事件触发时的表达式计算过程 foreach (ObjectEvent ei in prog.Events) { if (ei.Event.EventHandlerType == typeof(SelectionChangedEventHandler)) { ei.Event.AddEventHandler(ei.Object, new SelectionChangedEventHandler((o1, e1) => { Assign(result); })); } else if (ei.Event.EventHandlerType == typeof(RoutedEventHandler)) { ei.Event.AddEventHandler(ei.Object, new RoutedEventHandler((o1, e1) => { Assign(result); })); } else if (ei.Event.EventHandlerType == typeof(AsyncCompletedEventHandler)) { ei.Event.AddEventHandler(ei.Object, new AsyncCompletedEventHandler((o1, e1) => { Assign(result); })); } else if (ei.Event.EventHandlerType == typeof(MouseButtonEventHandler)) { ei.Event.AddEventHandler(ei.Object, new MouseButtonEventHandler((o1, e1) => { Assign(result); })); } else if (ei.Event.EventHandlerType == typeof(RoutedPropertyChangedEventHandler <object>)) { ei.Event.AddEventHandler(ei.Object, new RoutedPropertyChangedEventHandler <object>((o1, e1) => { Assign(result); })); } else if (ei.Event.EventHandlerType == typeof(PopulatingEventHandler)) { ei.Event.AddEventHandler(ei.Object, new PopulatingEventHandler((o1, e1) => { Assign(result); })); } else if (ei.Event.EventHandlerType == typeof(RoutedPropertyChangedEventHandler <Boolean>)) { ei.Event.AddEventHandler(ei.Object, new RoutedPropertyChangedEventHandler <Boolean>((o1, e1) => { Assign(result); })); } else if (ei.Event.EventHandlerType == typeof(NotifyCollectionChangedEventHandler)) { ei.Event.AddEventHandler(ei.Object, new NotifyCollectionChangedEventHandler((o1, e1) => { Assign(result); })); } else if (ei.Event.EventHandlerType == typeof(EventHandler)) { ei.Event.AddEventHandler(ei.Object, new EventHandler((ol, el) => { Assign(result); })); } else { throw new Exception("不支持这种事件类型:" + ei.Event.EventHandlerType); } } } }