/// <summary>
        /// Detaches the event source.
        /// </summary>
        /// <param name="item">The item to detach.</param>
        protected void DetachEventSource([NotNull] INotifyPropertyChanged item)
        {
            var sourceType = item.GetType();

            if (EventSources.TryGetValue(sourceType, out var oldListern))
            {
                oldListern?.Detach();
                EventSources.Remove(sourceType);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Detaches the event source.
        /// </summary>
        /// <param name="item">The item to detach.</param>
        protected void DetachEventSource([NotNull] INotifyPropertyChanged item)
        {
            var sourceType = item.GetType();

            if (EventSources.TryGetValue(sourceType, out var oldListener) && oldListener.TryGetTarget(out var target))
            {
                target.PropertyChanged -= RelaySource_PropertyChanged;
                EventSources.Remove(sourceType);
            }
        }
        /// <summary>
        /// Relays the property changed events of the source object.
        /// The properties to relay must be declared with the <see cref="RelayedEventAttribute"/>.
        /// </summary>
        /// <param name="source">The source.</param>
        protected void RelayEventsOf([NotNull] INotifyPropertyChanged source)
        {
            Contract.Requires(source != null);

            var sourceType = source.GetType();
            if (RelayMapping.Keys.All(key => key?.IsAssignableFrom(sourceType) != true))
                throw new InvalidOperationException(@"This class has no property with a RelayedEventAttribute for the type " + sourceType);

            INotifyPropertyChanged oldSource;
            if (EventSources.TryGetValue(sourceType, out oldSource) && (oldSource != null))
                oldSource.PropertyChanged -= RelaySource_PropertyChanged;

            source.PropertyChanged += RelaySource_PropertyChanged;
            EventSources[sourceType] = source;
        }
        /// <summary>
        /// Relays the property changed events of the source object.
        /// The properties to relay must be declared with the <see cref="RelayedEventAttribute"/>.
        /// </summary>
        /// <param name="source">The source.</param>
        protected void RelayEventsOf([NotNull] INotifyPropertyChanged source)
        {
            var sourceType = source.GetType();

            if (RelayMapping.Keys.All(key => key?.IsAssignableFrom(sourceType) != true))
            {
                throw new InvalidOperationException(@"This class has no property with a RelayedEventAttribute for the type " + sourceType);
            }

            if (EventSources.TryGetValue(sourceType, out var oldListern))
            {
                oldListern?.Detach();
            }

            var newListener = new WeakEventListener(this, source, OnWeakEvent, OnAttach, OnDetach);

            EventSources[sourceType] = newListener;
        }
Beispiel #5
0
        /// <summary>
        /// Relays the property changed events of the source object.
        /// The properties to relay must be declared with the <see cref="RelayedEventAttribute"/>.
        /// </summary>
        /// <param name="source">The source.</param>
        protected void RelayEventsOf([NotNull] INotifyPropertyChanged source)
        {
            var sourceType = source.GetType();

            if (RelayMapping.Keys.All(key => key?.IsAssignableFrom(sourceType) != true))
            {
                throw new InvalidOperationException(@"This class has no property with a RelayedEventAttribute for the type " + sourceType);
            }

            if (EventSources.TryGetValue(sourceType, out var oldListener) && oldListener.TryGetTarget(out var oldTarget))
            {
                oldTarget.PropertyChanged -= RelaySource_PropertyChanged;
            }

            source.PropertyChanged += RelaySource_PropertyChanged;

            EventSources[sourceType] = new WeakReference <INotifyPropertyChanged>(source);
        }