private string ComputeDisplayName()
        {
#if VS2008
            var displayName = PropertyDescriptor.DisplayName;
#else
            var displayAttribute = PropertyGridUtilities.GetAttribute <DisplayAttribute>(PropertyDescriptor);
            var displayName      = (displayAttribute != null) ? displayAttribute.GetName() : PropertyDescriptor.DisplayName;
#endif

            var attribute = PropertyGridUtilities.GetAttribute <ParenthesizePropertyNameAttribute>(PropertyDescriptor);
            if ((attribute != null) && attribute.NeedParenthesis)
            {
                displayName = "(" + displayName + ")";
            }

            return(displayName);
        }
예제 #2
0
        private List <PropertyItem> GetObjectProperties(object instance)
        {
            var propertyItems = new List <PropertyItem>();

            if (instance == null)
            {
                return(propertyItems);
            }

            try
            {
                PropertyDescriptorCollection descriptors = PropertyGridUtilities.GetPropertyDescriptors(instance);

                if (!AutoGenerateProperties)
                {
                    List <PropertyDescriptor> specificProperties = new List <PropertyDescriptor>();
                    foreach (PropertyDefinition pd in PropertyDefinitions)
                    {
                        foreach (PropertyDescriptor descriptor in descriptors)
                        {
                            if (descriptor.Name == pd.Name)
                            {
                                specificProperties.Add(descriptor);
                                break;
                            }
                        }
                    }

                    descriptors = new PropertyDescriptorCollection(specificProperties.ToArray());
                }

                foreach (PropertyDescriptor descriptor in descriptors)
                {
                    if (descriptor.IsBrowsable)
                    {
                        propertyItems.Add(PropertyGridUtilities.CreatePropertyItem(descriptor, instance, this, descriptor.Name));
                    }
                }
            }
            catch (Exception)
            {
                //TODO: handle this some how
            }

            return(propertyItems);
        }
예제 #3
0
        internal object ComputeDisplayOrderForItem(object item)
        {
            PropertyDescriptor pd = item as PropertyDescriptor;

#if !VS2008
            var displayAttribute = PropertyGridUtilities.GetAttribute <DisplayAttribute>(PropertyDescriptor);
            if (displayAttribute != null)
            {
                var order = displayAttribute.GetOrder();
                if (order.HasValue)
                {
                    return(displayAttribute.GetOrder());
                }
            }
#endif

            List <PropertyOrderAttribute> list = pd.Attributes.OfType <PropertyOrderAttribute>().ToList();

            if (list.Count > 0)
            {
                this.ValidatePropertyOrderAttributes(list);

                if (this.IsPropertyGridCategorized)
                {
                    var attribute = list.FirstOrDefault(x => ((x.UsageContext == UsageContextEnum.Categorized) ||
                                                              (x.UsageContext == UsageContextEnum.Both)));
                    if (attribute != null)
                    {
                        return(attribute.Order);
                    }
                }
                else
                {
                    var attribute = list.FirstOrDefault(x => ((x.UsageContext == UsageContextEnum.Alphabetical) ||
                                                              (x.UsageContext == UsageContextEnum.Both)));
                    if (attribute != null)
                    {
                        return(attribute.Order);
                    }
                }
            }

            // Max Value. Properties with no order will be displayed last.
            return(int.MaxValue);
        }
예제 #4
0
        internal static PropertyItem CreatePropertyItem(PropertyDescriptor property, object instance, PropertyGrid grid, string bindingPath)
        {
            PropertyItem propertyItem = new PropertyItem(instance, property, grid, bindingPath);

            var binding = new Binding(bindingPath)
            {
                Source = instance,
                ValidatesOnExceptions = true,
                ValidatesOnDataErrors = true,
                Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay
            };

            propertyItem.SetBinding(PropertyItem.ValueProperty, binding);

            propertyItem.Editor = PropertyGridUtilities.GetTypeEditor(propertyItem, grid.EditorDefinitions);

            return(propertyItem);
        }
예제 #5
0
        internal static FrameworkElement GetTypeEditor(PropertyItem propertyItem, EditorDefinitionCollection editorDefinitions)
        {
            //first check for an attribute editor
            FrameworkElement editor = PropertyGridUtilities.GetAttibuteEditor(propertyItem);

            //now look for a custom editor based on editor definitions
            if (editor == null)
            {
                editor = PropertyGridUtilities.GetCustomEditor(propertyItem, editorDefinitions);
            }

            //guess we have to use the default editor
            if (editor == null)
            {
                editor = PropertyGridUtilities.CreateDefaultEditor(propertyItem);
            }

            return(editor);
        }
예제 #6
0
        private void LoadProperties(bool isCategorized)
        {
            if (_propertyItemsCache == null)
            {
                return;
            }

            //clear any filters first
            Filter = String.Empty;

            if (isCategorized)
            {
                Properties = PropertyGridUtilities.GetCategorizedProperties(_propertyItemsCache);
            }
            else
            {
                Properties = PropertyGridUtilities.GetAlphabetizedProperties(_propertyItemsCache);
            }
        }
예제 #7
0
        internal object ComputeDescriptionForItem(object item)
        {
            PropertyDescriptor pd = item as PropertyDescriptor;

            //We do not simply rely on the "Description" property of PropertyDescriptor
            //since this value is cached by PropertyDescriptor and the localized version
            //(e.g., LocalizedDescriptionAttribute) value can dynamicaly change.
#if !VS2008
            var displayAttribute = PropertyGridUtilities.GetAttribute <DisplayAttribute>(pd);
            if (displayAttribute != null)
            {
                return(displayAttribute.GetDescription());
            }
#endif

            var descriptionAtt = PropertyGridUtilities.GetAttribute <DescriptionAttribute>(pd);
            return((descriptionAtt != null)
              ? descriptionAtt.Description
              : pd.Description);
        }
        internal static Predicate <object> CreateFilter(string text, IList <PropertyItem> PropertyItems, IPropertyContainer propertyContainer)
        {
            Predicate <object> filter = null;

            if (!string.IsNullOrEmpty(text))
            {
                filter = (item) =>
                {
                    var property = item as PropertyItem;
                    if (property.DisplayName != null)
                    {
#if !VS2008
                        var displayAttribute = PropertyGridUtilities.GetAttribute <DisplayAttribute>(property.PropertyDescriptor);
                        if (displayAttribute != null)
                        {
                            var canBeFiltered = displayAttribute.GetAutoGenerateFilter();
                            if (canBeFiltered.HasValue && !canBeFiltered.Value)
                            {
                                return(false);
                            }
                        }
#endif
                        property.HighlightedText = property.DisplayName.ToLower().Contains(text.ToLower()) ? text : null;
                        return(property.HighlightedText != null);
                    }

                    return(false);
                };
            }
            else
            {
                ClearFilterSubItems(PropertyItems.ToList());
            }

            return(filter);
        }
        internal FrameworkElement GenerateChildrenEditorElement(PropertyItem propertyItem)
        {
            FrameworkElement editorElement      = null;
            DescriptorPropertyDefinitionBase pd = propertyItem.DescriptorDefinition;
            object      definitionKey           = null;
            Type        definitionKeyAsType     = definitionKey as Type;
            ITypeEditor editor = null;

            if (editor == null)
            {
                editor = pd.CreateAttributeEditor();
            }

            if (editor != null)
            {
                editorElement = editor.ResolveEditor(propertyItem);
            }


            if ((editorElement == null) && (definitionKey == null) && (propertyItem.PropertyDescriptor != null))
            {
                editorElement = this.GenerateCustomEditingElement(propertyItem.PropertyDescriptor.Name, propertyItem);
            }

            if (editorElement == null && definitionKeyAsType == null)
            {
                editorElement = this.GenerateCustomEditingElement(propertyItem.PropertyType, propertyItem);
            }

            if (this.PropertyContainer != null && this.PropertyContainer.EditorDefinitions != null)
            {
                foreach (var edef in this.PropertyContainer.EditorDefinitions.Where(x => x != null && x.HasAttribute != null))
                {
                    if (propertyItem.PropertyDescriptor.Attributes.Cast <Attribute>().Any(x => x.GetType() == edef.HasAttribute))
                    {
                        return(edef.GenerateEditingElementInternal(propertyItem));
                    }
                }
            }

            if (editorElement == null)
            {
                if (pd.IsReadOnly)
                {
                    editor = new TextBlockEditor();
                }

                // Fallback: Use a default type editor.
                if (editor == null)
                {
                    editor = (definitionKeyAsType != null)
          ? PropertyGridUtilities.CreateDefaultEditor(definitionKeyAsType, null, propertyItem)
          : pd.CreateDefaultEditor(propertyItem);
                }

                Debug.Assert(editor != null);

                editorElement = editor.ResolveEditor(propertyItem);
            }

            return(editorElement);
        }
예제 #10
0
        protected override void GenerateSubPropertiesCore(Action <IEnumerable <PropertyItem> > updatePropertyItemsCallback)
        {
            var propertyItems = new List <PropertyItem>();

            if (SelectedObject != null)
            {
                try
                {
                    var descriptors = new List <PropertyDescriptor>();
                    {
                        descriptors = ObjectContainerHelperBase.GetPropertyDescriptors(SelectedObject, this.PropertyContainer.HideInheritedProperties);
                    }

                    foreach (var descriptor in descriptors)
                    {
                        var  propertyDef = this.GetPropertyDefinition(descriptor);
                        bool isBrowsable = false;

                        var isPropertyBrowsable = this.PropertyContainer.IsPropertyVisible(descriptor);
                        if (isPropertyBrowsable.HasValue)
                        {
                            isBrowsable = isPropertyBrowsable.Value;
                        }
                        else
                        {
#if !VS2008
                            var displayAttribute = PropertyGridUtilities.GetAttribute <DisplayAttribute>(descriptor);
                            if (displayAttribute != null)
                            {
                                var autoGenerateField = displayAttribute.GetAutoGenerateField();
                                isBrowsable = this.PropertyContainer.AutoGenerateProperties &&
                                              ((autoGenerateField.HasValue && autoGenerateField.Value) || !autoGenerateField.HasValue);
                            }
                            else
#endif
                            {
                                isBrowsable = descriptor.IsBrowsable && this.PropertyContainer.AutoGenerateProperties;
                            }

                            if (propertyDef != null)
                            {
                                isBrowsable = propertyDef.IsBrowsable.GetValueOrDefault(isBrowsable);
                            }
                        }

                        if (isBrowsable)
                        {
                            var prop = this.CreatePropertyItem(descriptor, propertyDef);
                            if (prop != null)
                            {
                                propertyItems.Add(prop);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    //TODO: handle this some how
                    Debug.WriteLine("Property creation failed.");
                    Debug.WriteLine(e.StackTrace);
                }
            }

            updatePropertyItemsCallback.Invoke(propertyItems);
        }
예제 #11
0
 private T GetAttribute <T>() where T : Attribute
 {
     return(PropertyGridUtilities.GetAttribute <T>(PropertyDescriptor));
 }
예제 #12
0
 internal override ITypeEditor CreateDefaultEditor(PropertyItem propertyItem)
 {
     return(PropertyGridUtilities.CreateDefaultEditor(PropertyDescriptor.PropertyType, PropertyDescriptor.Converter, propertyItem));
 }
예제 #13
0
 private string GetDefaultPropertyName(object selectedObject)
 {
     return(PropertyGridUtilities.GetDefaultPropertyName(selectedObject));
 }
 protected override ITypeEditor CreateDefaultEditor()
 {
     return(PropertyGridUtilities.CreateDefaultEditor(PropertyDescriptor.PropertyType, PropertyDescriptor.Converter));
 }