示例#1
0
        /// <summary>
        /// The create view model.
        /// </summary>
        /// <param name="instance">
        /// The instance.
        /// </param>
        /// <param name="descriptor">
        /// The descriptor.
        /// </param>
        /// <returns>
        /// </returns>
        public virtual PropertyViewModel CreateViewModel(object instance, PropertyDescriptor descriptor)
        {
            PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(instance);

            PropertyViewModel propertyViewModel = null;

            string optionalPropertyName;

            if (this.IsOptional(descriptor, out optionalPropertyName))
            {
                propertyViewModel = new OptionalPropertyViewModel(
                    instance, descriptor, optionalPropertyName, this.owner);
            }

            if (this.IsPassword(descriptor))
            {
                propertyViewModel = new PasswordPropertyViewModel(instance, descriptor, this.owner);
            }

            if (this.IsResettable(descriptor))
            {
                propertyViewModel = new ResettablePropertyViewModel(instance, descriptor, this.owner);
            }

            if (this.UsePropertyPattern != null)
            {
                string             usePropertyName = string.Format(this.UsePropertyPattern, descriptor.Name);
                PropertyDescriptor useDescriptor   = pdc.Find(usePropertyName, false);
                if (useDescriptor != null)
                {
                    propertyViewModel = new OptionalPropertyViewModel(instance, descriptor, useDescriptor, this.owner);
                }
            }

            bool showHeader;

            if (this.IsWide(descriptor, out showHeader))
            {
                propertyViewModel = new WidePropertyViewModel(instance, descriptor, showHeader, this.owner);
            }

            // If bool properties should be shown as checkbox only (no header label), we create a CheckBoxPropertyViewModel
            if (descriptor.PropertyType == typeof(bool) && this.owner != null && !this.owner.ShowBoolHeader)
            {
                propertyViewModel = new CheckBoxPropertyViewModel(instance, descriptor, this.owner);
            }

            double        min, max, largeChange, smallChange;
            double        tickFrequency;
            bool          snapToTicks;
            TickPlacement tickPlacement;

            if (this.IsSlidable(
                    descriptor,
                    out min,
                    out max,
                    out largeChange,
                    out smallChange,
                    out tickFrequency,
                    out snapToTicks,
                    out tickPlacement))
            {
                propertyViewModel = new SlidablePropertyViewModel(instance, descriptor, this.owner)
                {
                    SliderMinimum       = min,
                    SliderMaximum       = max,
                    SliderLargeChange   = largeChange,
                    SliderSmallChange   = smallChange,
                    SliderSnapToTicks   = snapToTicks,
                    SliderTickFrequency = tickFrequency,
                    SliderTickPlacement = tickPlacement
                };
            }

            // FilePath
            string filter, defaultExtension;
            bool   useOpenDialog;

            if (this.IsFilePath(descriptor, out filter, out defaultExtension, out useOpenDialog))
            {
                propertyViewModel = new FilePathPropertyViewModel(instance, descriptor, this.owner)
                {
                    Filter = filter, DefaultExtension = defaultExtension, UseOpenDialog = useOpenDialog
                };
            }

            // DirectoryPath
            if (this.IsDirectoryPath(descriptor))
            {
                propertyViewModel = new DirectoryPathPropertyViewModel(instance, descriptor, this.owner);
            }

            if (this.IsEnum(descriptor))
            {
                propertyViewModel = new EnumPropertyViewModel(instance, descriptor, this.owner);
            }

            // Default property (using textbox)
            if (propertyViewModel == null)
            {
                var tp = new PropertyViewModel(instance, descriptor, this.owner);
                propertyViewModel = tp;
            }

            // Check if the AutoUpdatingText attribute is set (this will select the template that has a text binding using UpdateSourceTrigger=PropertyChanged)
            if (this.IsAutoUpdatingText(descriptor))
            {
                propertyViewModel.AutoUpdateText = true;
            }

            propertyViewModel.FormatString = this.GetFormatString(descriptor);

            // var ha = AttributeHelper.GetFirstAttribute<HeightAttribute>(descriptor);
            // if (ha != null)
            // propertyViewModel.Height = ha.Height;
            propertyViewModel.Height    = this.GetHeight(descriptor);
            propertyViewModel.MaxLength = this.GetMaxLength(descriptor);

            if (propertyViewModel.Height > 0 || this.IsMultilineText(descriptor))
            {
                propertyViewModel.AcceptsReturn = true;
                propertyViewModel.TextWrapping  = TextWrapping.Wrap;
            }

            var soa = AttributeHelper.GetFirstAttribute <SortIndexAttribute>(descriptor);

            if (soa != null)
            {
                propertyViewModel.SortIndex = soa.SortIndex;
            }

            if (this.IsEnabledPattern != null)
            {
                string isEnabledName = string.Format(this.IsEnabledPattern, descriptor.Name);
                propertyViewModel.IsEnabledDescriptor = pdc.Find(isEnabledName, false);
            }

            if (this.IsVisiblePattern != null)
            {
                string isVisibleName = string.Format(this.IsVisiblePattern, descriptor.Name);
                propertyViewModel.IsVisibleDescriptor = pdc.Find(isVisibleName, false);
            }

            return(propertyViewModel);
        }
        /// <summary>
        /// The create view model.
        /// </summary>
        /// <param name="instance">
        /// The instance.
        /// </param>
        /// <param name="descriptor">
        /// The descriptor.
        /// </param>
        /// <returns>
        /// </returns>
        public virtual PropertyViewModel CreateViewModel(object instance, PropertyDescriptor descriptor)
        {
            PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(instance);

            PropertyViewModel propertyViewModel = null;

            string optionalPropertyName;
            if (this.IsOptional(descriptor, out optionalPropertyName))
            {
                propertyViewModel = new OptionalPropertyViewModel(
                    instance, descriptor, optionalPropertyName, this.owner);
            }

            if (this.IsPassword(descriptor))
            {
                propertyViewModel = new PasswordPropertyViewModel(instance, descriptor, this.owner);
            }

            if (this.IsResettable(descriptor))
            {
                propertyViewModel = new ResettablePropertyViewModel(instance, descriptor, this.owner);
            }

            if (this.UsePropertyPattern != null)
            {
                string usePropertyName = string.Format(this.UsePropertyPattern, descriptor.Name);
                PropertyDescriptor useDescriptor = pdc.Find(usePropertyName, false);
                if (useDescriptor != null)
                {
                    propertyViewModel = new OptionalPropertyViewModel(instance, descriptor, useDescriptor, this.owner);
                }
            }

            bool showHeader;
            if (this.IsWide(descriptor, out showHeader))
            {
                propertyViewModel = new WidePropertyViewModel(instance, descriptor, showHeader, this.owner);
            }

            // If bool properties should be shown as checkbox only (no header label), we create a CheckBoxPropertyViewModel
            if (descriptor.PropertyType == typeof(bool) && this.owner != null && !this.owner.ShowBoolHeader)
            {
                propertyViewModel = new CheckBoxPropertyViewModel(instance, descriptor, this.owner);
            }

            double min, max, largeChange, smallChange;
            double tickFrequency;
            bool snapToTicks;
            TickPlacement tickPlacement;
            if (this.IsSlidable(
                descriptor,
                out min,
                out max,
                out largeChange,
                out smallChange,
                out tickFrequency,
                out snapToTicks,
                out tickPlacement))
            {
                propertyViewModel = new SlidablePropertyViewModel(instance, descriptor, this.owner)
                    {
                        SliderMinimum = min,
                        SliderMaximum = max,
                        SliderLargeChange = largeChange,
                        SliderSmallChange = smallChange,
                        SliderSnapToTicks = snapToTicks,
                        SliderTickFrequency = tickFrequency,
                        SliderTickPlacement = tickPlacement
                    };
            }

            // FilePath
            string filter, defaultExtension;
            bool useOpenDialog;
            if (this.IsFilePath(descriptor, out filter, out defaultExtension, out useOpenDialog))
            {
                propertyViewModel = new FilePathPropertyViewModel(instance, descriptor, this.owner)
                    {
                       Filter = filter, DefaultExtension = defaultExtension, UseOpenDialog = useOpenDialog
                    };
            }

            // DirectoryPath
            if (this.IsDirectoryPath(descriptor))
            {
                propertyViewModel = new DirectoryPathPropertyViewModel(instance, descriptor, this.owner);
            }

            if (this.IsEnum(descriptor))
            {
                propertyViewModel = new EnumPropertyViewModel(instance, descriptor, this.owner);
            }

            // Default property (using textbox)
            if (propertyViewModel == null)
            {
                var tp = new PropertyViewModel(instance, descriptor, this.owner);
                propertyViewModel = tp;
            }

            // Check if the AutoUpdatingText attribute is set (this will select the template that has a text binding using UpdateSourceTrigger=PropertyChanged)
            if (this.IsAutoUpdatingText(descriptor))
            {
                propertyViewModel.AutoUpdateText = true;
            }

            propertyViewModel.FormatString = this.GetFormatString(descriptor);

            // var ha = AttributeHelper.GetFirstAttribute<HeightAttribute>(descriptor);
            // if (ha != null)
            // propertyViewModel.Height = ha.Height;
            propertyViewModel.Height = this.GetHeight(descriptor);
            propertyViewModel.MaxLength = this.GetMaxLength(descriptor);

            if (propertyViewModel.Height > 0 || this.IsMultilineText(descriptor))
            {
                propertyViewModel.AcceptsReturn = true;
                propertyViewModel.TextWrapping = TextWrapping.Wrap;
            }

            var soa = AttributeHelper.GetFirstAttribute<SortIndexAttribute>(descriptor);
            if (soa != null)
            {
                propertyViewModel.SortIndex = soa.SortIndex;
            }

            if (this.IsEnabledPattern != null)
            {
                string isEnabledName = string.Format(this.IsEnabledPattern, descriptor.Name);
                propertyViewModel.IsEnabledDescriptor = pdc.Find(isEnabledName, false);
            }

            if (this.IsVisiblePattern != null)
            {
                string isVisibleName = string.Format(this.IsVisiblePattern, descriptor.Name);
                propertyViewModel.IsVisibleDescriptor = pdc.Find(isVisibleName, false);
            }

            return propertyViewModel;
        }