Ejemplo n.º 1
0
        /// <summary>
        /// Sets the property value, ensures bindings are updated and validates (if <paramref name="propertyValidation"/> is set).<para/>
        /// Updates will be skipped (except for validation) if the <paramref name="newValue"/> equals the previous property value.
        /// </summary>
        /// <typeparam name="T">The type of the changed property.</typeparam>
        /// <param name="propertyField">The property field which contains the old value.</param>
        /// <param name="newValue">The new value to set.</param>
        /// <param name="propertyValidation">An optional function used for validation of the changed property. It must return a collection of error messages.</param>
        /// <param name="propertyName">The name of the property which has been changed. When omitted the property name will be the member name of the caller (which it is when called from the view model property setter).</param>
        /// <returns>Returns <c>true</c> if the new value was set.</returns>
        protected bool SetProperty <T>(ref T propertyField, T newValue, Func <T, IEnumerable <string> >?propertyValidation = null, [CallerMemberName] string?propertyName = null)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                Debug.Fail($"{nameof(ViewModel)}.{nameof(SetProperty)} has been called with a null or empty {nameof(propertyName)}.");
                return(false);
            }

            // stop when the new and old value equal
            if (EqualityComparer <T> .Default.Equals(propertyField, newValue))
            {
                return(false);
            }

            // if the value was a view model then make sure to update its ParentViewModel
            if (propertyName != nameof(ParentViewModel) && typeof(ViewModel).IsAssignableFrom(typeof(T)) && newValue == null)
            {
                (propertyField as ViewModel) !.ParentViewModel = null;
            }

            // otherwise set the new value for the property
            propertyField = newValue;

            // if the value was a view model then make sure to update its ParentViewModel
            if (propertyName != nameof(ParentViewModel) && typeof(ViewModel).IsAssignableFrom(typeof(T)) && newValue != null)
            {
                (newValue as ViewModel) !.ParentViewModel = this;
            }

            // validate the new value if needed
            AddPropertyErrors(propertyName !, propertyValidation?.Invoke(newValue));

            // inform bindings about the changed property
            RaisePropertyChanged(propertyName);

            // set the IsDirty flag to true (unless it is forbidden for this property name)
            if (!AlwaysIgnoredDirtyProperties.Contains(propertyName) && IgnoredDirtyProperties != null && !IgnoredDirtyProperties.Contains(propertyName))
            {
                IsDirty = true;

                // bubble the IsDirty flag to any ParentViewModel found
                if (ParentViewModel != null)
                {
                    ParentViewModel.IsDirty = true;
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        protected override void PostSetProperty <T>(ref T propertyField, T newValue, Func <T, IEnumerable <string> >?propertyValidation = null, [CallerMemberName] string?propertyName = null)
        {
            // if the value was a view model then make sure to update its ParentViewModel
            if (propertyName != nameof(ParentViewModel) && typeof(ViewModel).IsAssignableFrom(typeof(T)) && newValue != null)
            {
                (newValue as ViewModel) !.ParentViewModel = this;
            }

            // set the IsDirty flag to true (unless it is forbidden for this property name)
            if (!AlwaysIgnoredDirtyProperties.Contains(propertyName) && IgnoredDirtyProperties != null && !IgnoredDirtyProperties.Contains(propertyName))
            {
                IsDirty = true;

                // bubble the IsDirty flag to any ParentViewModel found
                if (ParentViewModel != null)
                {
                    ParentViewModel.IsDirty = true;
                }
            }
        }