Catel.MVVM.Views.ViewToViewModelMappingHelper.InitializeViewModel C# (CSharp) Method

InitializeViewModel() private method

Initializes the specified view model.
private InitializeViewModel ( IViewModel viewModel ) : void
viewModel IViewModel The view model.
return void
        private void InitializeViewModel(IViewModel viewModel)
        {
            var viewModelType = ObjectToStringHelper.ToTypeString(viewModel);

            Log.Debug("Initializing view model '{0}'", viewModelType);

            UninitializeViewModel(_previousViewModel);

            _previousViewModel = viewModel;

            if (viewModel != null)
            {
                // If there are mappings, sync them in the right way
                var viewModelContainerType = ViewModelContainerType;
                foreach (var mapping in _viewToViewModelMappingContainers[viewModelContainerType].GetAllViewToViewModelMappings())
                {
                    try
                    {
                        if ((mapping.MappingType == ViewToViewModelMappingType.TwoWayViewWins) ||
                            (mapping.MappingType == ViewToViewModelMappingType.ViewToViewModel))
                        {
                            TransferValueFromViewToViewModel(viewModel, mapping.ViewPropertyName, mapping.ViewModelPropertyName);
                        }
                        else if ((mapping.MappingType == ViewToViewModelMappingType.TwoWayViewModelWins) ||
                                 (mapping.MappingType == ViewToViewModelMappingType.ViewModelToView))
                        {
                            TransferValueFromViewModelToView(viewModel, mapping.ViewPropertyName, mapping.ViewModelPropertyName);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, "Failed to transfer value from view property '{0}' to the view model property '{1}' for the ViewToViewModelMapping", 
                            mapping.ViewPropertyName, mapping.ViewModelPropertyName);
                    }
                }

                viewModel.PropertyChanged += OnViewModelPropertyChanged;
            }

            Log.Debug("Initialized view model '{0}'", viewModelType);
        }