private void TargetListenStatusChanged(BindingTarget target, bool status) { if (status) { try { ((INotifyPropertyChanged)target.Target).PropertyChanged += TargetPropertyChanged; } catch { //do nothing } } else { try { ((INotifyPropertyChanged)target.Target).PropertyChanged -= TargetPropertyChanged; } catch { //do nothing } } }
/// <summary> /// Binds the property of an object implementing <see cref="INotifyPropertyChanged"/> to the current item, /// allowing for updating the property when a value changes in the object. /// </summary> /// <param name="target">The target object</param> /// <param name="propertyName">The name of the property to be bound</param> /// <param name="listen">Indicates whether to listen for property changes in the object</param> /// <returns></returns> public BindingItem BindObservableTarget(INotifyPropertyChanged target, string propertyName, bool listen = true) { BindingItem inst = BindTarget(target, propertyName); BindingTarget tar = BindingTargets.Find(x => x.Target == target); tar.ListenStatusChanged += TargetListenStatusChanged; tar.Listen = listen; tar.IsObservable = true; return(inst); }
/// <summary> /// Correctly unbinds a <see cref="BindingTarget"/> from the current item. /// </summary> /// <param name="bindingTarget">The target to be removed</param> /// <returns>True if the target was found and successfuly removed</returns> public bool UnbindTarget(BindingTarget bindingTarget) { if (BindingTargets.Remove(bindingTarget)) { if (bindingTarget.IsObservable) { bindingTarget.Listen = false; bindingTarget.ListenStatusChanged -= TargetListenStatusChanged; } return(true); } return(false); }
/// <summary> /// Binds the property of the <paramref name="target"/> to the locale item. /// </summary> /// <param name="target">The object whose property is to be bound</param> /// <param name="propertyName">The name of the property to be bound</param> /// <returns>The current binding item with the target added</returns> public BindingItem BindTarget(object target, string propertyName) { if (BindingTargets == null) { BindingTargets = new List <BindingTarget>(); } BindingTarget item = new BindingTarget { PropertyName = propertyName, Target = target, IsObservable = false }; BindingTargets.Add(item); return(this); }
/// <summary> /// Searches for the <see cref="BindingTarget"/> of the <paramref name="target"/> object /// and correctly unbinds it's property from the current binding item. /// </summary> /// <param name="target">The object who's property is to be unbinded</param> /// <param name="propertyName">The name of the property to be unbinded</param> /// <returns>True if the property was found and successfully removed</returns> public bool UnbindTarget(object target, string propertyName) { BindingTarget bindingTarget = BindingTargets.Find(x => x.Target == target && x.PropertyName == propertyName); return(UnbindTarget(bindingTarget)); }