【发布时间】:2019-10-17 13:45:17
【问题描述】:
语言:C# .Net Framework 4.6.1
操作系统:Window 7 Home 64 位
谁能告诉我属性网格需要什么类才能显示它的属性?
我尝试只继承和实现 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;
}
}
}
【问题讨论】:
标签: c# winforms dynamic propertygrid