gpt4 book ai didi

c# - 带有自定义 PropertyDescriptor 的 PropertyGrid

转载 作者:行者123 更新时间:2023-11-30 12:13:18 24 4
gpt4 key购买 nike

我一直在努力让自定义 PropertyDescriptors 以我想要的方式使用 PropertyGrid。

前提:

  • 我有一个名为“Animal”的类,其中包含属性 Age , Type , Location , 和 Name .
  • 我有另一个名为“AnimalGroup”的类,其中包含一个 List<Animal>存放动物的地方。包含此列表的类成员称为 Members .
  • 在程序的用户界面中,PropertyGrid表单元素将列出分配给 PropertGrid 的 SelectedObject 的 AnimalGroup 中的动物。属性(property)。

第三个项目符号我可以很容易地开始工作(见下图)。

PropertyGrid showing the members of an AnimalGroup.

AnimalGroup 类实现了 ICustomTypeDescriptor Members中实现动物列表功能的接口(interface)类(class)成员。

不幸的是,正如您在上图中所见,属性值只是从 Animal 的 ToString 方法返回的值。

我想让用户能够编辑 PropertyGrid 中每个动物的成员(年龄、类型、位置和名称)。我怎样才能做到这一点?我假设需要某种类型的自定义编辑器。然而,老实说,我不知道从哪里开始,因为谷歌搜索并没有为此提供太多帮助。

这是我达到这一点所遵循的教程:

如果您需要,这是我的代码(使用 .NET 4.0):

Animal.cs

public class Animal
{
private String _Name;

public String Name
{
get { return _Name; }
set { _Name = value; }
}

private String _Type;

public String Type
{
get { return _Type; }
set { _Type = value; }
}
private String _Age;

public String Age
{
get { return _Age; }
set { _Age = value; }
}
private String _Location;

public String Location
{
get { return _Location; }
set { _Location = value; }
}

public override string ToString()
{
return this.Name + " - " + this.Type;
}
}

AnimalGroup.cs

public class AnimalGroup : ICustomTypeDescriptor
{
public List<Animal> Members;

public AnimalGroup()
{
this.Members = new List<Animal>();
}

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

public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}

public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}

public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptor[] pds = new PropertyDescriptor[this.Members.Count];

int i = 0;
foreach (Animal a in this.Members)
{
pds[i] = new AnimalPropertyDescriptor(a, attributes);

i++;
}


return new PropertyDescriptorCollection(pds);
}

public PropertyDescriptorCollection GetProperties()
{
return this.GetProperties(new Attribute[0]);
}

public object GetPropertyOwner(PropertyDescriptor pd)
{
return this.Members;
}
}

AnimalPropertyDescriptor.cs

public class AnimalPropertyDescriptor : PropertyDescriptor
{
private Animal property;

public AnimalPropertyDescriptor(Animal target, Attribute[] attrs)
: base(target.Name, attrs)
{
this.property = target;
}

public override bool CanResetValue(object component)
{
return false;
}

public override Type ComponentType
{
get { return null; }
}

public override object GetValue(object component)
{
return property;
}

public override bool IsReadOnly
{
get { return false; }
}

public override Type PropertyType
{
get { return this.property.GetType(); }
}

public override void ResetValue(object component)
{
// Not relevant.
}

public override void SetValue(object component, object value)
{
this.property = (Animal)value;
}

public override bool ShouldSerializeValue(object component)
{
return false;
}
}

仅供引用,我知道代码有点荒谬(有名字的动物、动物群等)。

最佳答案

看起来你缺少一个转换器:

internal class AnimalConverter : ExpandableObjectConverter {

public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture,
object value,
Type destinationType) {

if (destinationType == typeof(string) && value is Animal) {
Animal a = (Animal)value;
return a.ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}
}

然后装饰你的动物:

[TypeConverter(typeof(AnimalConverter))]
public class Animal {...}

找到这个代码项目 Customized display of collection data in a PropertyGrid有帮助。

关于c# - 带有自定义 PropertyDescriptor 的 PropertyGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11892064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com