private void AssignValue(IObservablePropertyBinding <T> dst, IObservablePropertyBinding <T> src) { // only do the assignment if the value is actually different // otherwise we may get infinite recursion in the bi-directional case if (!object.Equals(dst.PropertyValue, src.PropertyValue)) { dst.PropertyValue = src.PropertyValue; } }
/// <summary> /// Constructor. /// </summary> /// <param name="primary">The primary property, which serves as the subject.</param> /// <param name="secondary">The secondary property, which observes the subject and tracks its value.</param> /// <param name="bidirectional">If true, the primary property will also track the value of the secondary property.</param> private ObservablePropertyCoupler(IObservablePropertyBinding <T> primary, IObservablePropertyBinding <T> secondary, bool bidirectional) { Platform.CheckForNullReference(primary, "primary"); Platform.CheckForNullReference(secondary, "secondary"); _primary = primary; _secondary = secondary; _bidirectional = bidirectional; // set up the coupling _primary.PropertyChanged += PrimaryChangedEventHandler; if (_bidirectional) { _secondary.PropertyChanged += SecondaryChangedEventHandler; } // initialize secondary value from primary _secondary.PropertyValue = _primary.PropertyValue; }
/// <summary> /// Establishes the coupling between the specified primary and secondary properties. /// </summary> /// <remarks> /// The value of the secondary property will be initialized to the value of the primary property, /// and will continue to be synchronized for the duration of the lifetime of this object. The coupling is /// optionally bi-directional, in which case changes made to the secondary property are also propagated /// back to the primary property. To remove the coupling at any point in the future, retain the returned /// object, and call Dispose() on it to remove the coupling. /// </remarks> /// <param name="primary">The primary property, which serves as the subject.</param> /// <param name="secondary">The secondary property, which observes the subject and tracks its value.</param> /// <param name="bidirectional">If true, the primary property will also track the value of the secondary property.</param> /// <returns>A property coupler object, which can optionally be retained for eventual disposal.</returns> public static ObservablePropertyCoupler <T> Couple(IObservablePropertyBinding <T> primary, IObservablePropertyBinding <T> secondary, bool bidirectional) { return(new ObservablePropertyCoupler <T>(primary, secondary, bidirectional)); }
/// <summary> /// Establishes the coupling between the specified primary and secondary properties. /// </summary> /// <remarks> /// The value of the secondary property will be initialized to the value of the primary property, /// and will continue to be synchronized for the duration of the lifetime of this object. /// To remove the coupling at any point in the future, retain the returned object, and /// call Dispose() on it to remove the coupling. /// </remarks> /// <param name="primary">The primary property, which serves as the subject.</param> /// <param name="secondary">The secondary property, which observes the subject and tracks its value.</param> /// <returns>A property coupler object, which can optionally be retained for eventual disposal.</returns> public static ObservablePropertyCoupler <T> Couple(IObservablePropertyBinding <T> primary, IObservablePropertyBinding <T> secondary) { return(new ObservablePropertyCoupler <T>(primary, secondary, false)); }