/// <summary>
        /// Allows interested objects to be notified when this property changes.
        /// </summary>
        public override void AddValueChanged(object component, EventHandler handler)
        {
            if (component == null)
            {
                throw new ArgumentNullException(nameof(component));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            // If there's an event called <propertyname>Changed, hook the caller's handler directly up to that on the component
            EventDescriptor changedEvent = ChangedEventValue;

            if (changedEvent != null && changedEvent.EventType.IsInstanceOfType(handler))
            {
                changedEvent.AddEventHandler(component, handler);
            }

            // Otherwise let the base class add the handler to its ValueChanged event for this component
            else
            {
                // Special case: If this will be the FIRST handler added for this component, and the component implements
                // INotifyPropertyChanged, the property descriptor must START listening to the generic PropertyChanged event
                if (GetValueChangedHandler(component) == null)
                {
                    EventDescriptor iPropChangedEvent = IPropChangedEventValue;
                    iPropChangedEvent?.AddEventHandler(component, new PropertyChangedEventHandler(OnINotifyPropertyChanged));
                }

                base.AddValueChanged(component, handler);
            }
        }
Example #2
0
        /// <summary>
        /// Returns an observable sequence of events from the specified <paramref name="event"/> descriptor.
        /// </summary>
        /// <param name="event">The descriptor from which to create an observable sequence of changed notifications.</param>
        /// <param name="source">The object to which the <paramref name="event"/> belongs.</param>
        /// <returns>An observable sequence of events.</returns>
        public static IObservable <EventPattern <EventArgs> > EventRaised(
            this EventDescriptor @event,
            object source)
        {
            Contract.Requires(@event != null);
            Contract.Requires(source != null);
            Contract.Ensures(Contract.Result <IObservable <EventPattern <EventArgs> > >() != null);

            return(@event.EventType == typeof(EventHandler)
        ? Observable.FromEventPattern <EventHandler, EventArgs>(
                       handler => handler.Invoke,
                       handler => @event.AddEventHandler(source, handler),
                       handler => @event.RemoveEventHandler(source, handler))
        : new EventProxyObservable(source, @event));
        }
Example #3
0
		internal void SetDataSource (object new_data_source)
		{
			if (changed_event != null)
				changed_event.RemoveEventHandler (data_source, property_value_changed_handler);

			data_source = new_data_source;

			if (property_name != null) {
				prop_desc = TypeDescriptor.GetProperties (data_source).Find (property_name, true);

				if (prop_desc == null)
					return;

				changed_event = TypeDescriptor.GetEvents (data_source).Find (property_name + "Changed", false);
				if (changed_event != null) {
					property_value_changed_handler = new EventHandler (PropertyValueChanged);
					changed_event.AddEventHandler (data_source, property_value_changed_handler);
				}
			}
		}
 public TriggerEventPropertyObservation(EventDescriptor changedEvent, object obj)
 {
     if (changedEvent.EventType == typeof (EventHandler<EventArgs>))
     {
         changedEvent.AddEventHandler(obj, (EventHandler<EventArgs>) OnPropertyValueChanged);
     }
     else if (changedEvent.EventType == typeof (EventHandler))
     {
         changedEvent.AddEventHandler(obj, (EventHandler) OnPropertyValueChanged);
     }
 }