/// <summary> /// To ensure connection didn't happen twice it first checks if /// adaptor with that object already exists. /// If Adaptor with this object specified as Target is already /// present then object was connected already anyway /// </summary> /// <param name="aObject"> /// Object being connected <see cref="System.Object"/> /// </param> /// <remarks> /// If object being connected to is type of PropertyChangedEventHandler /// then connection to PropertyChanged happens, otherwise it /// connects to all PropertyChangedEventHandler properties /// </remarks> public static void ConnectEvent(object aObject) { if (aObject == null) { return; } if (DataSourceController.CountDataSourceOwners(aObject) > 1) { return; } if (aObject is INotifyPropertyChanged) { (aObject as INotifyPropertyChanged).PropertyChanged += OnObjectPropertyChanged; } else { // Connect to all PropertyChangedEventHandler properties foreach (EventInfo ev in aObject.GetType().GetEvents()) { if (ev.EventHandlerType is PropertyChangedEventHandler) { ev.AddEventHandler(aObject, OnObjectPropertyChanged); } } } }
/// <summary> /// Disconect only happens if only one Adaptor has this object /// specified as Target. /// If not then connection is still valid /// </summary> /// <param name="aObject"> /// Object being diconnected <see cref="System.Object"/> /// </param> /// <remarks> /// If object being disconnected from is type of PropertyChangedEventHandler /// then disconnection from PropertyChanged happens, otherwise it /// disconnects from all PropertyChangedEventHandler properties /// </remarks> public static void DisconnectEvent(object aObject) { if (aObject == null) { return; } if (DataSourceController.CountDataSourceOwners(aObject) > 1) { return; } if (aObject is INotifyPropertyChanged) { (aObject as INotifyPropertyChanged).PropertyChanged -= OnObjectPropertyChanged; } else { // Disconnect from all PropertyChangedEventHandler properties foreach (EventInfo ev in aObject.GetType().GetEvents()) { if (ev.EventHandlerType is PropertyChangedEventHandler) { // ev.RemoveEventHandler (aObject, new PropertyChangedEventHandler(ObjectPropertyChanged)); ev.RemoveEventHandler(aObject, OnObjectPropertyChanged); } } } }