/// <summary> /// Initializes a new instance of the <see cref="Binding"/> class. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> /// <param name="mode">The mode.</param> /// <param name="converter">The converter.</param> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="target"/> is <c>null</c>.</exception> public Binding(BindingParty source, BindingParty target, BindingMode mode = BindingMode.TwoWay, IValueConverter converter = null) { Argument.IsNotNull("source", source); Argument.IsNotNull("target", target); Mode = mode; Converter = converter; _source = source; _target = target; Initialize(); }
private void UpdateBinding(BindingParty source, BindingParty target, bool useConvertBack) { if (_isUpdatingBinding) { return; } if (!EnsureBindingLifetime()) { Uninitialize(); return; } try { _isUpdatingBinding = true; Log.Debug("Updating binding '{0}' => '{1}'", source, target); var newValue = source.GetPropertyValue(); var converter = Converter; if (converter != null) { if (useConvertBack) { newValue = converter.ConvertBack(newValue, typeof (object), ConverterParameter, CultureInfo.CurrentCulture); } else { newValue = converter.Convert(newValue, typeof(object), ConverterParameter, CultureInfo.CurrentCulture); } } if (ReferenceEquals(newValue, ConverterHelper.UnsetValue)) { Log.Debug("Skipping update because new value is 'ConverterHelper.UnsetValue'"); return; } target.SetPropertyValue(newValue); ValueChanged.SafeInvoke(this); } catch (Exception ex) { Log.Error(ex, "Failed to update binding"); } finally { _isUpdatingBinding = false; } }
/// <summary> /// Uninitializes this binding. /// </summary> protected override void Uninitialize() { _source.ValueChanged -= OnSourceValueChanged; _source.Dispose(); _source = null; _target.ValueChanged -= OnTargetValueChanged; _target.Dispose(); _target = null; Log.Debug("Uninitialized binding '{0}'", this); }