Search code examples
c#winformsdynamicpropertygrid

What is required for a class so that a property grid shows its properties?


Language: C# .Net Framework 4.6.1

Operating System: Window 7 Home 64 bit

Can someone tell me what is required of a class for a property grid to display it's properties?

I tried just inheriting and implementing ICustomTypeDescriptor but nothing is showing up.

I would like the properties grid to also try to parse the values instead of just accepting a string, and I would like to be able to specify a category.

here is my code..

using System;
using System.Collections;
using System.Dynamic;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Collections.Specialized;
using System.ComponentModel;

namespace PureCore.Library.Data
{

    public class Property : DynamicObject, ICustomTypeDescriptor
    {
        private Dictionary<string, object> Properties = new Dictionary<string, object>();

        public override bool TryGetMember(GetMemberBinder binder, out object value)
        {
            if (Properties.ContainsKey(binder.Name))
            {
                value = Properties[binder.Name];
                return true;
            }
            return base.TryGetMember(binder, out value);
        }
        public override bool TrySetMember(SetMemberBinder binder,     object value)
        {  
            if (Properties.ContainsKey(binder.Name))
            {
                Properties[binder.Name] = value;
                return true;
            }

            Properties.Add(binder.Name, value);
            return true;
        }

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }
        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }
        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }
        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }
        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }
        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }
        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }
        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }
        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }
        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(this, true);
        }
        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return TypeDescriptor.GetProperties(this, attributes, true);
        }
        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }
    }
}
using PureCore.Library.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Editor
{
    public partial class EnemyEditor : Form
    {
        dynamic property = new Property();
        public EnemyEditor()
        {
            InitializeComponent();
            property.HI = "Hello";
            propertyGrid1.SelectedObject = property;
        }

    }
}

Solution

  • You have to add a bit more code, specifically you must give the grid a list of properties explicitly, deriving from PropertyDescriptor, something like this:

    public class CustomType : DynamicObject, ICustomTypeDescriptor
    {
        private readonly Dictionary<string, object> _properties = new Dictionary<string, object>();
    
        public AttributeCollection GetAttributes() => new AttributeCollection();
        public string GetClassName() => null;
        public string GetComponentName() => null;
        public TypeConverter GetConverter() => null;
        public EventDescriptor GetDefaultEvent() => null;
        public PropertyDescriptor GetDefaultProperty() => null;
        public object GetEditor(Type editorBaseType) => null;
        public EventDescriptorCollection GetEvents() => GetEvents(null);
        public EventDescriptorCollection GetEvents(Attribute[] attributes) => new EventDescriptorCollection(null);
        public PropertyDescriptorCollection GetProperties() => GetProperties(null);
        public object GetPropertyOwner(PropertyDescriptor pd) => this;
        public PropertyDescriptorCollection GetProperties(Attribute[] attributes) => new PropertyDescriptorCollection(_properties.Select(p => new CustomProperty(p.Key, p.Value, "Mycats")).ToArray());
    
        public override bool TryGetMember(GetMemberBinder binder, out object value) => _properties.TryGetValue(binder.Name, out value);
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            _properties[binder.Name] = value;
            return true;
        }
    }
    
    public class CustomProperty : PropertyDescriptor
    {
        public CustomProperty(string name, object value, string category)
            : base(name, new Attribute[] { new CategoryAttribute(category) })
        {
            Value = value;
        }
    
        public object Value { get; set; }
        public override Type ComponentType => typeof(CustomType);
        public override bool IsReadOnly => false;
        public override Type PropertyType => Value != null ? Value.GetType() : typeof(object);
        public override bool CanResetValue(object component) => false;
        public override object GetValue(object component) => Value;
        public override void ResetValue(object component) => throw new NotSupportedException();
        public override void SetValue(object component, object value) => Value = value;
        public override bool ShouldSerializeValue(object component) => false;
    }