View model base for MVVM implementations. This class is based on the ModelBase, and supports all common interfaces used by WPF.
This view model base does not add any services. The technique specific implementation should take care of that (such as WPF, Silverlight, etc).
Inheritance: ModelBase, IViewModel, INotifyableViewModel, IRelationalViewModel, IUniqueIdentifyable
        /// <summary>
        /// Creates a property descriptor for a specific view model.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <returns>The <see cref="ViewModelPropertyDescriptor"/>.</returns>
        /// <exception cref="ArgumentException">The <paramref name="propertyName"/> is <c>null</c> or whitespace.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="propertyType"/> is <c>null</c>.</exception>
        public static ViewModelPropertyDescriptor CreatePropertyDescriptor(ViewModelBase viewModel, string propertyName, Type propertyType)
        {
            Argument.IsNotNullOrWhitespace("propertyName", propertyName);
            Argument.IsNotNull("propertyType", propertyType);

            return new ViewModelPropertyDescriptor(viewModel, propertyName, propertyType);
        }
        /// <summary>
        /// Creates a property descriptor for a specific view model.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <param name="attributes">The attributes.</param>
        /// <returns>The <see cref="ViewModelPropertyDescriptor" />.</returns>
        /// <exception cref="ArgumentException">The <paramref name="propertyName" /> is <c>null</c> or whitespace.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="propertyType" /> is <c>null</c>.</exception>
        public static ViewModelPropertyDescriptor CreatePropertyDescriptor(ViewModelBase viewModel, string propertyName, Type propertyType, Attribute[] attributes)
        {
            Argument.IsNotNullOrWhitespace("propertyName", propertyName);
            Argument.IsNotNull("propertyType", propertyType);

            if (attributes == null)
            {
                attributes = new Attribute[] { };
            }

            return new ViewModelPropertyDescriptor(viewModel, propertyName, propertyType, attributes);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelPropertyDescriptor" /> class.
        /// </summary>
        /// <param name="viewModel">The view model. Can be <c>null</c> for generic property definitions.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <param name="attributes">The attributes.</param>
        /// <exception cref="ArgumentException">The <paramref name="propertyName" /> is <c>null</c> or whitespace.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="propertyType" /> is <c>null</c>.</exception>
        /// <remarks>Must be kept internal because it contains special generic options such as a null view model.</remarks>
        internal ViewModelPropertyDescriptor(ViewModelBase viewModel, string propertyName, Type propertyType, Attribute[] attributes)
            : base(propertyName, attributes)
        {
            Argument.IsNotNullOrWhitespace("propertyName", propertyName);
            Argument.IsNotNull("propertyType", propertyType);

            _viewModel = viewModel;
            _viewModelType = (viewModel != null) ? viewModel.GetType() : null;
            _propertyName = propertyName;
            _propertyType = propertyType;

            if (_viewModelType != null)
            {
                string cacheKey = string.Format("{0}_{1}", _viewModelType.FullName, propertyName);
                _propertyInfo = _propertyInfoCache.GetFromCacheOrFetch(cacheKey, () => _viewModelType.GetPropertyEx(propertyName));
            }
        }
        /// <summary>
        /// Creates the property descriptor for a specfic view model based on an existing property descriptor.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="propertyDescriptor">The property descriptor.</param>
        /// <returns>The <see cref="ViewModelPropertyDescriptor" />.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="propertyDescriptor" /> is <c>null</c>.</exception>
        public static ViewModelPropertyDescriptor CreatePropertyDescriptor(ViewModelBase viewModel, ViewModelPropertyDescriptor propertyDescriptor)
        {
            Argument.IsNotNull("propertyDescriptor", propertyDescriptor);

            var attributes = new List<Attribute>();
            if (propertyDescriptor.Attributes != null)
            {
                foreach (var attribute in propertyDescriptor.Attributes)
                {
                    var attrib = attribute as Attribute;
                    if (attrib != null)
                    {
                        attributes.Add(attrib);
                    }
                }
            }

            return CreatePropertyDescriptor(viewModel, propertyDescriptor.Name, propertyDescriptor.PropertyType, attributes.ToArray());
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ViewModelPropertyDescriptor"/> class.
        /// </summary>
        /// <param name="viewModel">The view model. Can be <c>null</c> for generic property definitions.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <remarks>
        /// Must be kept internal because it contains special generic options such as a null view model.
        /// </remarks>
        /// <exception cref="ArgumentException">The <paramref name="propertyName"/> is <c>null</c> or whitespace.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="propertyType"/> is <c>null</c>.</exception>
        internal ViewModelPropertyDescriptor(ViewModelBase viewModel, string propertyName, Type propertyType)
            : base(propertyName, null)
        {
            Argument.IsNotNullOrWhitespace("propertyName", propertyName);
            Argument.IsNotNull("propertyType", propertyType);

            //Log.Debug("Created property descriptor for '{0}' as type '{1}'", propertyName, propertyType.Name);

            _viewModel = viewModel;
            _viewModelType = (viewModel != null) ? viewModel.GetType() : null;
            _propertyName = propertyName;
            _propertyType = propertyType;

            if (_viewModelType != null)
            {
                string cacheKey = string.Format("{0}_{1}", _viewModelType.FullName, propertyName);
                _propertyInfo = _propertyInfoCache.GetFromCacheOrFetch(cacheKey, () => _viewModelType.GetPropertyEx(propertyName));
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Gets the nested validation context. If <paramref name="recursive"/> is <c>true</c>, the validation context returned
        /// will include all validation from all registered children.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="recursive">If set to <c>true</c>, the validation context will be merged with all children.</param>
        /// <returns>
        /// A combined <see cref="IValidationContext"/> of all the child view models and the <paramref name="viewModel"/> itself.
        /// </returns>
        /// <remarks>
        /// This method does not check for arguments for performance reasons and because it's private.
        /// </remarks>
        private static IValidationContext GetNestedValidationContext(ViewModelBase viewModel, bool recursive)
        {
            var validationContext = new ValidationContext();

            validationContext.SynchronizeWithContext(viewModel.ValidationContext, true);

            if (recursive)
            {
                foreach (var childViewModel in viewModel.ChildViewModels)
                {
                    var childAsViewModelBase = childViewModel as ViewModelBase;
                    if (childAsViewModelBase != null)
                    {
                        validationContext.SynchronizeWithContext(GetNestedValidationContext(childAsViewModelBase, true), true);
                    }
                }
            }

            return validationContext;
        }
        /// <summary>
        /// Creates the property descriptor for a specfic view model based on an existing property descriptor. 
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="propertyDescriptor">The property descriptor.</param>
        /// <returns>The <see cref="ViewModelPropertyDescriptor"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="propertyDescriptor"/> is <c>null</c>.</exception>
        public static ViewModelPropertyDescriptor CreatePropertyDescriptor(ViewModelBase viewModel, ViewModelPropertyDescriptor propertyDescriptor)
        {
            Argument.IsNotNull("propertyDescriptor", propertyDescriptor);

            return CreatePropertyDescriptor(viewModel, propertyDescriptor.Name, propertyDescriptor.PropertyType);
        }