Catel.MVVM.ViewModelBase.UpdateExplicitViewModelToModelMappings C# (CSharp) Method

UpdateExplicitViewModelToModelMappings() protected method

Updates the view model to model mappings that are defined as ViewModelToModelMode.Explicit.
protected UpdateExplicitViewModelToModelMappings ( ) : void
return void
        protected void UpdateExplicitViewModelToModelMappings()
        {
            Log.Debug("Updating all explicit view model to model mappings");

            var explicitMappings = (from mapping in _viewModelToModelMap
                                    where mapping.Value.Mode == ViewModelToModelMode.Explicit
                                    select mapping.Value);

            foreach (var mapping in explicitMappings)
            {
                var model = _modelObjects[mapping.ModelProperty];
                if (model != null)
                {
                    object value = GetValue(mapping.ViewModelProperty);
                    var modelValues = mapping.Converter.ConvertBack(value, this);
                    for (int i = 0; i < mapping.ValueProperties.Length; i++)
                    {
                        if (PropertyHelper.TrySetPropertyValue(model, mapping.ValueProperties[i], modelValues[i], false))
                        {
                            Log.Debug("Updated property '{0}' on model type '{1}' to '{2}'", mapping.ValueProperties, model.GetType().Name, ObjectToStringHelper.ToString(value));
                        }
                        else
                        {
                            Log.Warning("Failed to set property '{0}' on model type '{1}'", mapping.ValueProperties, model.GetType().Name);
                        }
                    }
                }
            }

            Log.Debug("Updated all explicit view model to model mappings");
        }