/// <summary> /// Adds the event by automatically retrieving the event args type. /// </summary> /// <param name="bindingParty">The binding party.</param> /// <param name="eventName">Name of the event.</param> /// <exception cref="ArgumentNullException">The <paramref name="bindingParty"/> is <c>null</c>.</exception> /// <exception cref="ArgumentException">The <paramref name="bindingParty"/> is <c>null</c>.</exception> public static void AddEvent(this BindingParty bindingParty, string eventName) { Argument.IsNotNull("bindingParty", bindingParty); Argument.IsNotNullOrWhitespace("eventName", eventName); var instance = bindingParty.Instance; if (instance == null) { Log.ErrorAndThrowException <InvalidOperationException>("The BindingParty '{0}' is no longer alive, cannot add event '{1}'", bindingParty, eventName); } var instanceType = instance.GetType(); var eventInfo = instanceType.GetEventEx(eventName); if (eventInfo == null) { Log.ErrorAndThrowException <InvalidOperationException>("Event '{0}.{1}' does not exist", instanceType.Name, eventName); } var eventHandlerType = eventInfo.EventHandlerType; var eventArgsType = eventHandlerType.GetGenericArgumentsEx()[0]; var genericAddEventMethod = AddEventMethodInfo.MakeGenericMethod(eventArgsType); genericAddEventMethod.Invoke(bindingParty, new object[] { eventName }); }
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); }
/// <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(); }