class 需要什么才能使 属性 网格显示其属性?
What is required for a class so that a property grid shows its properties?
语言:C# .Net Framework 4.6.1
操作系统:Window 7 Home 64 位
谁能告诉我 class 需要什么才能使 属性 网格显示其属性?
我只是尝试继承和实现 ICustomTypeDescriptor,但没有显示任何内容。
我希望属性网格也尝试解析值而不是仅仅接受一个字符串,我希望能够指定一个类别。
这是我的代码..
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;
}
}
}
您必须添加更多代码,具体来说,您必须明确地为网格提供一个属性列表,从 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, "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;
}
语言:C# .Net Framework 4.6.1
操作系统:Window 7 Home 64 位
谁能告诉我 class 需要什么才能使 属性 网格显示其属性?
我只是尝试继承和实现 ICustomTypeDescriptor,但没有显示任何内容。
我希望属性网格也尝试解析值而不是仅仅接受一个字符串,我希望能够指定一个类别。
这是我的代码..
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;
}
}
}
您必须添加更多代码,具体来说,您必须明确地为网格提供一个属性列表,从 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, "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;
}