Ejemplo n.º 1
0
        /// <summary>
        /// Notifies any subscribers that the value of a <see cref="ModelProperty"/> has changed.
        /// </summary>
        protected virtual void OnModelPropertyChanged(ModelPropertyChangedEventArgs e)
        {
            if (e.Property.PropertyChangedHandler != null)
            {
                e.Property.PropertyChangedHandler(this, e);
            }

            OnPropertyChanged(e);

            if (e.Property.DependantProperties != null)
            {
                var changedProperties = new List <ModelPropertyChangedEventArgs>();
                foreach (var propertyKey in e.Property.DependantProperties)
                {
                    object currentValue;
                    if (_values.TryGetValue(propertyKey, out currentValue))
                    {
                        var property = ModelProperty.GetPropertyForKey(propertyKey);
                        var value    = ((ModelProperty.UnitializedValue)property.DefaultValue).GetValue(this);
                        if (!Object.Equals(value, currentValue))
                        {
                            SetValueCore(property, value);
                            changedProperties.Add(new ModelPropertyChangedEventArgs(property, currentValue, value));
                        }
                    }
                }

                foreach (var changedProperty in changedProperties)
                {
                    OnModelPropertyChanged(changedProperty);
                }
            }
        }
Ejemplo n.º 2
0
        internal void OnPropertyChanged(ModelPropertyChangedEventArgs e)
        {
            if (_propertyChangedHandlers.Count > 0)
            {
                WeakAction <object, ModelPropertyChangedEventArgs>[] handlers = null;
                lock (_lockObject)
                {
                    List <WeakAction <object, ModelPropertyChangedEventArgs> > propertyChangedHandlers;
                    if (_propertyChangedHandlers.TryGetValue(e.Property.Key, out propertyChangedHandlers))
                    {
                        for (int i = propertyChangedHandlers.Count - 1; i >= 0; i--)
                        {
                            if (!propertyChangedHandlers[i].IsAlive)
                            {
                                propertyChangedHandlers.RemoveAt(i);
                            }
                        }

                        if (propertyChangedHandlers.Count == 0)
                        {
                            _propertyChangedHandlers = _propertyChangedHandlers.Remove(e.Property.Key);
                        }
                        else
                        {
                            handlers = propertyChangedHandlers.ToArray();
                        }
                    }
                }

                if (handlers != null)
                {
                    foreach (var handler in handlers)
                    {
                        handler.Invoke(this, e);
                    }
                }
            }

            if (!String.IsNullOrEmpty(e.Property.PropertyName))
            {
                OnPropertyChanged(new PropertyChangedEventArgs(e.Property.PropertyName));
            }
        }