/// <summary> /// Initializes a new instance of the <see cref="ViewToViewModelMappingContainer"/> class. /// </summary> /// <param name="dependencyObject">The dependency object.</param> /// <exception cref="ArgumentNullException">The <paramref name="dependencyObject"/> is <c>null</c>.</exception> public ViewToViewModelMappingContainer(DependencyObject dependencyObject) { Argument.IsNotNull("dependencyObject", dependencyObject); var properties = dependencyObject.GetType().GetPropertiesEx(); foreach (var property in properties) { object[] viewToViewModelAttributes = property.GetCustomAttributesEx(typeof(ViewToViewModelAttribute), false); if (viewToViewModelAttributes.Length > 0) { Log.Debug("Property '{0}' is decorated with the ViewToViewModelAttribute, creating a mapping", property.Name); var viewToViewModelAttribute = (ViewToViewModelAttribute)viewToViewModelAttributes[0]; string propertyName = property.Name; string viewModelPropertyName = (string.IsNullOrEmpty(viewToViewModelAttribute.ViewModelPropertyName)) ? propertyName : viewToViewModelAttribute.ViewModelPropertyName; var mapping = new ViewToViewModelMapping(propertyName, viewModelPropertyName, viewToViewModelAttribute.MappingType); // Store it (in 2 dictionaries for high-speed access) _viewToViewModelMappings.Add(property.Name, mapping); _viewModelToViewMappings.Add(viewModelPropertyName, mapping); } } }
/// <summary> /// Initializes a new instance of the <see cref="ViewToViewModelMappingContainer" /> class. /// </summary> /// <param name="viewModelContainer">The view.</param> /// <exception cref="ArgumentNullException">The <paramref name="viewModelContainer" /> is <c>null</c>.</exception> public ViewToViewModelMappingContainer(IViewModelContainer viewModelContainer) { Argument.IsNotNull("view", viewModelContainer); var properties = viewModelContainer.GetType().GetPropertiesEx(); foreach (var property in properties) { object[] viewToViewModelAttributes = property.GetCustomAttributesEx(typeof(ViewToViewModelAttribute), false); if (viewToViewModelAttributes.Length > 0) { Log.Debug("Property '{0}' is decorated with the ViewToViewModelAttribute, creating a mapping", property.Name); var viewToViewModelAttribute = (ViewToViewModelAttribute)viewToViewModelAttributes[0]; string propertyName = property.Name; string viewModelPropertyName = (string.IsNullOrEmpty(viewToViewModelAttribute.ViewModelPropertyName)) ? propertyName : viewToViewModelAttribute.ViewModelPropertyName; var mapping = new ViewToViewModelMapping(propertyName, viewModelPropertyName, viewToViewModelAttribute.MappingType); // Store it (in 2 dictionaries for high-speed access) _viewToViewModelMappings.Add(property.Name, mapping); _viewModelToViewMappings.Add(viewModelPropertyName, mapping); } } }
/// <summary> /// Called when a property on the view model container has changed. /// </summary> /// <param name="sender">The view model container.</param> /// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param> private void OnViewModelContainerPropertyChanged(object sender, PropertyChangedEventArgs e) { var viewModelContainerType = ViewModelContainerType; if (_viewToViewModelMappingContainers[viewModelContainerType].ContainsViewToViewModelMapping(e.PropertyName)) { ViewToViewModelMapping mapping = _viewToViewModelMappingContainers[viewModelContainerType].GetViewToViewModelMapping(e.PropertyName); if (_ignoredViewChanges.Contains(mapping.ViewPropertyName)) { Log.Debug("Ignored property changed event for view.'{0}'", mapping.ViewPropertyName); } else { if ((mapping.MappingType == ViewToViewModelMappingType.TwoWayDoNothing) || (mapping.MappingType == ViewToViewModelMappingType.TwoWayViewWins) || (mapping.MappingType == ViewToViewModelMappingType.TwoWayViewModelWins) || (mapping.MappingType == ViewToViewModelMappingType.ViewToViewModel)) { TransferValueFromViewToViewModel(CurrentViewModel, mapping.ViewPropertyName, mapping.ViewModelPropertyName); } } } }