示例#1
0
        private void ObserveProperty(PropertyInfo propertyInfo, EventHandler <ObservablePropertyChangedEventArgs> handler)
        {
            ObservedProperty observedProperty;

            if (!_observedProperties.TryGetValue(propertyInfo.Name, out observedProperty))
            {
                observedProperty = new ObservedProperty(propertyInfo);
                observedProperty.CurrentValue = propertyInfo.GetValue(this, null);
                observedProperty.Handlers     = new EventHandler <ObservablePropertyChangedEventArgs> [1];

                _observedProperties = _observedProperties.Add(propertyInfo.Name, observedProperty);
            }

            int idx = 0;

            while (idx < observedProperty.Handlers.Length && observedProperty.Handlers[idx] != null)
            {
                idx++;
            }

            if (idx == observedProperty.Handlers.Length)
            {
                var newHandlers = new EventHandler <ObservablePropertyChangedEventArgs> [idx * 2];
                Array.Copy(observedProperty.Handlers, 0, newHandlers, 0, idx);
                observedProperty.Handlers = newHandlers;
            }

            observedProperty.Handlers[idx] = handler;
        }
示例#2
0
        /// <summary>
        /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
        /// </summary>
        /// <param name="key">The object to use as the key of the element to add.</param>
        /// <param name="value">The object to use as the value of the element to add.</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception>
        /// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.</exception>
        public void Add(TKey key, TValue value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            _data = _data.Add(key, value);
        }
示例#3
0
        /// <summary>
        /// Registers the property pass through.
        /// </summary>
        /// <param name="sourcePropertyName">The property on <typeparamref name="TSource"/> to watch for changes.</param>
        /// <param name="targetPropertyName">The property on the target object to raise when the source property changed.</param>
        public void RegisterPropertyPassThrough(string sourcePropertyName, string targetPropertyName)
        {
            if (_propertyMap.Count == 0 && _source != null)
            {
                _source.PropertyChanged += SourcePropertyChanged;
            }

            // TODO: support multiple targets dependant on single source?
            _propertyMap = _propertyMap.Add(sourcePropertyName, targetPropertyName);
        }
示例#4
0
        /// <summary>
        /// Registers a callback to be invoked when the PropertyChanged event has been raised for the specified property.
        /// You should use the RegisterHandler method that accepts a lambda expression for compile-time reference validation.
        /// </summary>
        /// <param name="propertyName">The name of the property to watch.</param>
        /// <param name="handler">The callback to invoke when the property has changed.</param>
        /// <exception cref="ArgumentNullException"><paramref name="propertyName"/> or <paramref name="handler"/> is null.</exception>
        /// <exception cref="InvalidOperationException">A handler is already registered for <paramref name="propertyName"/>.</exception>
        public void RegisterHandler(string propertyName, PropertyChangedEventHandler handler)
        {
            if (String.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            _handlers = _handlers.Add(propertyName, new WeakAction <object, PropertyChangedEventArgs>(handler.Method, handler.Target));
        }
示例#5
0
        /// <summary>
        /// Adds the specified item to the collection.
        /// </summary>
        /// <param name="item">The item to add.</param>
        /// <param name="weight">The weight of the item. Compared to the weights of other items in the collection, determines the liklyhood of this item being selected.</param>
        public void Add(T item, int weight)
        {
            int oldWeight;

            if (_items.TryGetValue(item, out oldWeight))
            {
                _totalWeight -= oldWeight;
                _items        = _items.AddOrUpdate(item, weight);
            }
            else
            {
                _items = _items.Add(item, weight);
            }

            _totalWeight += weight;
        }
示例#6
0
        /// <summary>
        /// Binds a property on a model to the view model.
        /// </summary>
        /// <param name="viewModelProperty">View model property to bind.</param>
        /// <param name="binding">Information about how the view model property is bound.</param>
        public void SetBinding(ModelProperty viewModelProperty, ModelBinding binding)
        {
            ModelBinding oldBinding;

            if (_bindings.TryGetValue(viewModelProperty.Key, out oldBinding))
            {
                _bindings = _bindings.Remove(viewModelProperty.Key);
                if (!IsObserving(oldBinding.Source, oldBinding.SourceProperty))
                {
                    oldBinding.Source.RemovePropertyChangedHandler(oldBinding.SourceProperty, OnSourcePropertyChanged);
                }
            }
            else if (_selfBindings.TryGetValue(viewModelProperty.Key, out oldBinding))
            {
                _selfBindings = _selfBindings.Remove(viewModelProperty.Key);
            }

            if (binding != null)
            {
                if (ReferenceEquals(binding.Source, this))
                {
                    _selfBindings = _selfBindings.Add(viewModelProperty.Key, binding);
                    RefreshBinding(viewModelProperty.Key, binding);
                }
                else
                {
                    if (!IsObserving(binding.Source, binding.SourceProperty))
                    {
                        binding.Source.AddPropertyChangedHandler(binding.SourceProperty, OnSourcePropertyChanged);
                    }

                    _bindings = _bindings.Add(viewModelProperty.Key, binding);
                    RefreshBinding(viewModelProperty.Key, binding);
                }
            }
        }