/// <summary>
        /// Sets the properties.
        /// </summary>
        /// <param name="pi">
        /// The property item.
        /// </param>
        protected virtual void SetProperties(PropertyItem pi)
        {
            var pd         = pi.Descriptor;
            var properties = pi.Properties;

            var tabName      = this.DefaultTabName ?? pi.Instance.GetType().Name;
            var categoryName = this.DefaultCategoryName;

            // find the declaring type
            Type declaringType = pi.Descriptor.ComponentType;
            var  propertyInfo  = pi.Instance.GetType().GetProperty(pi.Descriptor.Name);

            if (propertyInfo != null)
            {
                declaringType = propertyInfo.DeclaringType;
            }

            if (declaringType != this.CurrentDeclaringType)
            {
                this.CurrentCategory = null;
            }

            this.CurrentDeclaringType = declaringType;

            if (!this.InheritCategories)
            {
                this.CurrentCategory = null;
            }

            var ca = AttributeHelper.GetFirstAttribute <CategoryAttribute>(pd);

            if (ca != null)
            {
                this.CurrentCategory = ca.Category;
                this.CurrentCategoryDeclaringType = declaringType;
            }

            var category = this.CurrentCategory ?? (this.DefaultCategoryName ?? pd.Category);

            if (category != null)
            {
                var items = category.Split('|');
                if (items.Length == 2)
                {
                    tabName      = items[0];
                    categoryName = items[1];
                }

                if (items.Length == 1)
                {
                    categoryName = items[0];
                }
            }

            var displayName = this.GetDisplayName(pd, declaringType);
            var description = this.GetDescription(pd, declaringType);

            pi.DisplayName = this.GetLocalizedString(displayName, declaringType);
            pi.Description = this.GetLocalizedDescription(description, declaringType);

            pi.Category            = this.GetLocalizedString(categoryName, this.CurrentCategoryDeclaringType);
            pi.CategoryDescription = this.GetLocalizedDescription(categoryName, this.CurrentCategoryDeclaringType);
            pi.Tab            = this.GetLocalizedString(tabName, this.CurrentCategoryDeclaringType);
            pi.TabDescription = this.GetLocalizedDescription(tabName, this.CurrentCategoryDeclaringType);

            // Find descriptors by convention
            pi.IsEnabledDescriptor = pi.GetDescriptor(string.Format(this.EnabledPattern, pd.Name));
            pi.IsVisibleDescriptor = pi.GetDescriptor(string.Format(this.VisiblePattern, pd.Name));
            pi.OptionalDescriptor  = pi.GetDescriptor(string.Format(this.OptionalPattern, pd.Name));

            pi.UseRadioButtons = pi.GetAttribute <RadioButtonsAttribute>() != null;
            var fp = pi.GetAttribute <FontPreviewAttribute>();

            pi.PreviewFonts = fp != null;
            if (fp != null)
            {
                pi.FontSize   = fp.Size;
                pi.FontWeight = fp.Weight;
                pi.FontFamilyPropertyDescriptor = pi.GetDescriptor(fp.FontFamilyPropertyName);
            }

            var fpa = pi.GetAttribute <FilePathAttribute>();

            pi.IsFilePath = fpa != null;
            if (fpa != null)
            {
                pi.FilePathFilter           = fpa.Filter;
                pi.FilePathDefaultExtension = fpa.DefaultExtension;
                pi.IsFileOpenDialog         = fpa.UseOpenDialog;
            }

            var dpa = pi.GetAttribute <DirectoryPathAttribute>();

            pi.IsDirectoryPath = dpa != null;

            foreach (var da in pi.GetAttributes <DataTypeAttribute>())
            {
                pi.DataTypes.Add(da.DataType);
                switch (da.DataType)
                {
                case DataType.MultilineText:
                    pi.AcceptsReturn = true;
                    break;

                case DataType.Password:
                    pi.IsPassword = true;
                    break;
                }
            }

            foreach (var da in pi.GetAttributes <ColumnAttribute>())
            {
                pi.Columns.Add(da);
            }

            var la = pi.GetAttribute <ListAttribute>();

            if (la != null)
            {
                pi.ListCanAdd               = la.CanAdd;
                pi.ListCanRemove            = la.CanRemove;
                pi.ListMaximumNumberOfItems = la.MaximumNumberOfItems;
            }

            var sia = pi.GetAttribute <SortIndexAttribute>();

            if (sia != null)
            {
                pi.SortIndex = sia.SortIndex;
            }

            var eba = pi.GetAttribute <EnableByAttribute>();

            if (eba != null)
            {
                pi.IsEnabledDescriptor = properties.Find(eba.PropertyName, false);
            }

            var vba = pi.GetAttribute <VisibleByAttribute>();

            if (vba != null)
            {
                pi.IsVisibleDescriptor = properties.Find(vba.PropertyName, false);
            }

            if (pi.Is(typeof(IList)))
            {
                pi.MinimumHeight   = 100;
                pi.MaximumHeight   = 240;
                pi.HeaderPlacement = HeaderPlacement.Above;
            }

            pi.IsOptional = pi.OptionalDescriptor != null;

            var oa = pi.GetAttribute <OptionalAttribute>();

            if (oa != null)
            {
                if (oa.PropertyName != null)
                {
                    pi.OptionalDescriptor = properties.Find(oa.PropertyName, false);
                }

                pi.IsOptional = true;
            }

            pi.IsComment = pi.GetAttribute <CommentAttribute>() != null;
            if (pi.IsComment)
            {
                pi.HeaderPlacement = HeaderPlacement.Hidden;
            }

            pi.IsEditable = pi.GetAttribute <IsEditableAttribute>() != null;

            pi.AutoUpdateText = pi.GetAttribute <AutoUpdateTextAttribute>() != null;

            var ispa = pi.GetAttribute <ItemsSourcePropertyAttribute>();

            if (ispa != null)
            {
                pi.ItemsSourceDescriptor = properties.Find(ispa.PropertyName, false);
            }

            var rpa = pi.GetAttribute <BasePathPropertyAttribute>();

            if (rpa != null)
            {
                pi.RelativePathDescriptor = properties.Find(rpa.BasePathPropertyName, false);
            }

            var fa = pi.GetAttribute <FilterPropertyAttribute>();

            if (fa != null)
            {
                pi.FilterDescriptor = properties.Find(fa.PropertyName, false);
            }

            var fsa = pi.GetAttribute <FormatStringAttribute>();

            if (fsa != null)
            {
                pi.FormatString = fsa.FormatString;
            }

            var coa = pi.GetAttribute <ConverterAttribute>();

            if (coa != null)
            {
                pi.Converter = Activator.CreateInstance(coa.ConverterType) as IValueConverter;
            }

            var sa = pi.GetAttribute <SlidableAttribute>();

            if (sa != null)
            {
                pi.IsSlidable          = true;
                pi.SliderMinimum       = sa.Minimum;
                pi.SliderMaximum       = sa.Maximum;
                pi.SliderSmallChange   = sa.SmallChange;
                pi.SliderLargeChange   = sa.LargeChange;
                pi.SliderSnapToTicks   = sa.SnapToTicks;
                pi.SliderTickFrequency = sa.TickFrequency;
            }

            var spa = pi.GetAttribute <SpinnableAttribute>();

            if (spa != null)
            {
                pi.IsSpinnable     = true;
                pi.SpinMinimum     = spa.Minimum;
                pi.SpinMaximum     = spa.Maximum;
                pi.SpinSmallChange = spa.SmallChange;
                pi.SpinLargeChange = spa.LargeChange;
            }

            var wpa = pi.GetAttribute <WidePropertyAttribute>();

            if (wpa != null)
            {
                pi.HeaderPlacement = wpa.ShowHeader ? HeaderPlacement.Above : HeaderPlacement.Hidden;
            }

            var wia = pi.GetAttribute <WidthAttribute>();

            if (wia != null)
            {
                pi.Width = wia.Width;
            }

            var hpa = pi.GetAttribute <HeaderPlacementAttribute>();

            if (hpa != null)
            {
                pi.HeaderPlacement = hpa.HeaderPlacement;
            }

            var ha = pi.GetAttribute <HeightAttribute>();

            if (ha != null)
            {
                pi.Height        = ha.Height;
                pi.MinimumHeight = ha.MinimumHeight;
                pi.MaximumHeight = ha.MaximumHeight;
                pi.AcceptsReturn = true;
            }
        }
Exemple #2
0
        /// <summary>
        /// The is password.
        /// </summary>
        /// <param name="descriptor">
        /// The descriptor.
        /// </param>
        /// <returns>
        /// The is password.
        /// </returns>
        protected virtual bool IsPassword(PropertyDescriptor descriptor)
        {
            var pa = AttributeHelper.GetFirstAttribute <DataTypeAttribute>(descriptor);

            return(pa != null && pa.DataType == DataType.Password);
        }
Exemple #3
0
        /// <summary>
        /// The is multiline text.
        /// </summary>
        /// <param name="descriptor">
        /// The descriptor.
        /// </param>
        /// <returns>
        /// The is multiline text.
        /// </returns>
        protected virtual bool IsMultilineText(PropertyDescriptor descriptor)
        {
            var dta = AttributeHelper.GetFirstAttribute <DataTypeAttribute>(descriptor);

            return(dta != null && dta.DataType == DataType.MultilineText);
        }
Exemple #4
0
        /// <summary>
        /// The is directory path.
        /// </summary>
        /// <param name="descriptor">
        /// The descriptor.
        /// </param>
        /// <returns>
        /// The is directory path.
        /// </returns>
        protected virtual bool IsDirectoryPath(PropertyDescriptor descriptor)
        {
            var dpa = AttributeHelper.GetFirstAttribute <DirectoryPathAttribute>(descriptor);

            return(dpa != null);
        }
Exemple #5
0
        /// <summary>
        /// The is auto updating text.
        /// </summary>
        /// <param name="descriptor">
        /// The descriptor.
        /// </param>
        /// <returns>
        /// The is auto updating text.
        /// </returns>
        protected virtual bool IsAutoUpdatingText(PropertyDescriptor descriptor)
        {
            var a = AttributeHelper.GetFirstAttribute <AutoUpdateTextAttribute>(descriptor);

            return(a != null);
        }
Exemple #6
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);
        }