为什么我的动态对象没有显示在属性网格中?

时间:2019-06-01 00:41:22

标签: c# winforms dynamic propertygrid

我刚尝试为Winforms应用程序实现自定义属性类,以便可以在propertygrid中使用它。

尽管我已经实现了ICustomTypeDescriptor

,但似乎属性没有显示

我不确定自己在做什么错,我想知道我需要做些什么才能使该对象显示在propertygrid中

我已经通过以下代码项目示例并仔细检查了stackoverflow和msdn帖子,尝试了许多实现此问题的尝试。

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;
        }

    }
}

我希望将该对象序列化到propertygrid中,并不允许我输入无效的数据(例如,字符串作为int)。

1 个答案:

答案 0 :(得分:0)

您必须添加更多的代码,特别是必须为网格赋予从PropertyDescriptor派生的属性列表,如下所示:

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)).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)
        : base(name, Array.Empty<Attribute>())
    {
        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;
}