Example #1
0
        /// <summary>
        /// Propagates changes from model property to view-model property.
        /// </summary>
        /// <typeparam name="TModelResult">Model property type</typeparam>
        /// <typeparam name="TViewModelResult">View-model property type</typeparam>
        /// <param name="modelProperty">Model property</param>
        /// <param name="viewModelProperty">View-model property</param>
        protected virtual void AssociateProperties <TModelResult, TViewModelResult>
            (Expression <Func <TModel, TModelResult> > modelProperty,
            Expression <Func <TViewModel, TViewModelResult> > viewModelProperty)
        {
            // Convert expressions to a property names
            string modelPropertyName     = ((MemberExpression)modelProperty.Body).Member.Name;
            string viewModelPropertyName = ((MemberExpression)viewModelProperty.Body).Member.Name;

            // Get handlers
            if (!assocPropsHandlers.ContainsKey(modelPropertyName))
            {
                assocPropsHandlers.Add(modelPropertyName, new PropertyDictionary());
            }
            var handlers = assocPropsHandlers[modelPropertyName];

            // Propagate model to view-model property change
            PropertyChangedEventHandler handler = (s, ea) =>
            {
                if (ea.PropertyName == modelPropertyName)
                {
                    BindingHelper.InternalNotifyPropertyChanged(viewModelPropertyName,
                                                                this, base.propertyChanged);
                }
            };

            // Add handler to property changed event
            Model.PropertyChanged += handler;

            // Add handler to handlers list
            handlers.Add(viewModelPropertyName, handler);
        }
Example #2
0
        /// <summary>
        /// Copies property values from clone to original.
        /// </summary>
        public void EndEdit()
        {
            // Return if BeginEdit not called first
            if (Copy == null)
            {
                return;
            }

            // Tranfer values from copy to original
            Copy.CopyValuesTo(Original);

            // Point entity to original
            Model = Original;

            // Clear copy
            Copy = null;

            // Notify IsEditing, IsDirty
            BindingHelper.InternalNotifyPropertyChanged("IsEditing",
                                                        this, base.propertyChanged);
            BindingHelper.InternalNotifyPropertyChanged("IsDirty",
                                                        this, base.propertyChanged);

            // Post-processing
            OnEndEdit();
        }
Example #3
0
        /// <summary>
        /// Caches a deep clone of the entity
        /// </summary>
        public void BeginEdit()
        {
            // Throw an exception if Entity not supplied
            if (Model == null)
            {
                throw new InvalidOperationException("Entity must be set");
            }

            // Return if we're already editing
            if (Copy != null)
            {
                return;
            }

            // Copy entity
            Original = Model;
            Copy     = Model.Clone();

            // Point entity to the copy
            Model = Copy;

            // Notify IsEditing, IsDirty
            BindingHelper.InternalNotifyPropertyChanged("IsEditing",
                                                        this, base.propertyChanged);
            BindingHelper.InternalNotifyPropertyChanged("IsDirty",
                                                        this, base.propertyChanged);

            // Post-processing
            OnBeginEdit();
        }
Example #4
0
        private void InternalValidateProperty(string propertyName, object value)
        {
            var  results = new List <ValidationResult>();
            bool isValid = Validator.TryValidateProperty(value, new ValidationContext(this, null, null)
            {
                MemberName = propertyName
            }, results);

            if (isValid)
            {
                RemoveErrors(propertyName);
            }
            else
            {
                AddErrors(propertyName, results);
            }
            NotifyErrorsChanged(propertyName);
            BindingHelper.InternalNotifyPropertyChanged("HasErrors", this, propertyChanged);
        }
Example #5
0
        /// <summary>
        /// Restores original
        /// </summary>
        public void CancelEdit()
        {
            // Return if BeginEdit not called first
            if (Copy == null)
            {
                return;
            }

            // Point entity to original
            Model = Original;

            // Clear copy
            Copy = null;

            // Notify IsEditing, IsDirty
            BindingHelper.InternalNotifyPropertyChanged("IsEditing",
                                                        this, base.propertyChanged);
            BindingHelper.InternalNotifyPropertyChanged("IsDirty",
                                                        this, base.propertyChanged);

            // Post-processing
            OnCancelEdit();
        }