public static DumbBinding SetBinding(DumbBindingTarget target, BindingBase bindingToSource) { lock (instances) { if (target == null) { throw new ArgumentNullException("target"); } DumbBinding result = new DumbBinding(bindingToSource, target); ClearBinding(target); object targetObject = target.TargetObject; if (targetObject == null) { throw new ArgumentException("target.TargetObject is no longer alive.", "target"); } KeyValuePair <int, int> key = new KeyValuePair <int, int>(target.GetHashCode(), targetObject.GetHashCode()); if (instances.ContainsKey(key)) { instances[key].Add(result); } else { instances.Add(key, new List <DumbBinding>() { result }); } return(result); } }
public static DumbBinding GetBinding(DumbBindingTarget target) { lock (instances) { if (target == null) { throw new ArgumentNullException("target"); } object targetObject = target.TargetObject; if (targetObject == null) { throw new ArgumentException("target.TargetObject is no longer alive.", "target"); } KeyValuePair <int, int> key = new KeyValuePair <int, int>(target.GetHashCode(), targetObject.GetHashCode()); if (instances.ContainsKey(key)) { return(instances[key].FirstOrDefault(binding => object.Equals(binding.Target, target))); } else { return(null); } } }
private DumbBinding(BindingBase bindingToSource, DumbBindingTarget target) { if (bindingToSource == null) { throw new ArgumentNullException("bindingToSource"); } if (target == null) { throw new ArgumentNullException("target"); } this.BindingToSource = bindingToSource; this.Target = target; target.PropertyChangedCallback = this.OnTargetPropertyChanged; BindingMode? bindingMode = null; Binding bindingToSourceAsBinding; MultiBinding bindingToSourceAsMultiBinding; if ((bindingToSourceAsBinding = bindingToSource as Binding) != null) { bindingMode = bindingToSourceAsBinding.Mode; this.SourcePropertyChangedHandle = PropertyChangedHandle.GetDistinctInstance(bindingToSourceAsBinding, this.OnSourceValueChanged); this.UpdateTarget(this.SourcePropertyChangedHandle.PropertyValue, false); } else if ((bindingToSourceAsMultiBinding = bindingToSource as MultiBinding) != null) { bindingMode = bindingToSourceAsMultiBinding.Mode; this.SourceMultiPropertyChangedHandle = MultiPropertyChangedHandle.GetDistinctInstance(bindingToSourceAsMultiBinding.Bindings.Cast <Binding>().ToArray(), this.OnSourceValuesChanged); this.UpdateTarget(this.MultiConvert(this.GetSourceValues()), false); } else { throw new NotSupportedException(String.Format("Binding type {0} not supported.", bindingToSource.GetType())); } if (bindingMode == BindingMode.OneWayToSource) { this.UpdateSource(); } }
public static void ClearBinding(DumbBindingTarget target) { if (target == null) { throw new ArgumentNullException("target"); } object targetObject = target.TargetObject; if (targetObject == null) { throw new ArgumentException("target.TargetObject is no longer alive.", "target"); } lock (instances) { KeyValuePair <int, int> key = new KeyValuePair <int, int>(target.GetHashCode(), targetObject.GetHashCode()); if (instances.ContainsKey(key)) { var list = instances[key]; for (int i = list.Count - 1; i >= 0; i--) { DumbBinding binding = list[i]; if (object.Equals(target, binding.Target) || binding.Target.TargetObject == null) { binding.Clear(); list.RemoveAt(i); } } if (list.Count == 0) { instances.Remove(key); } } } }