- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在努力让自定义 PropertyDescriptors 以我想要的方式使用 PropertyGrid。
前提:
Age
, Type
, Location
, 和 Name
.List<Animal>
存放动物的地方。包含此列表的类成员称为 Members
.PropertyGrid
表单元素将列出分配给 PropertGrid 的 SelectedObject
的 AnimalGroup 中的动物。属性(property)。第三个项目符号我可以很容易地开始工作(见下图)。
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/
我们正在使用 DynamicObject 创建动态属性,但随后我们想使用 PropertyGrid 来显示和编辑这些属性。 首先,我找到了this文章和这个one .我尝试使用第二篇文章的代码,但以更
我有一个数组字段的 PropertyDescriptor,如下所示。 Foo[] fooArray; 如何获取 Foo 的 PropertyDescriptor,以便能够获取该类的 getter 和
我有一组自定义 PropertyDescriptor,我也想添加类别,以便它们以更有条理的方式显示在 PropertyGrid 中。我希望每种类型的 PropertyDescriptor 都属于特定的
如何获取当前属性的 PropertyDescriptor?例如: [MyAttribute("SomeText")] public string MyProperty { get{....}
我正在通过实现 ICustomTypeDescriptor 自定义对象类型在 PropertyGrid 中的显示方式。我允许用户创建他们自己的自定义属性,这些属性存储在单个键和值字典中。我能够为这些值
.NET 上的 PropertyType 和 ComponentType 有什么区别 PropertyDescriptor ? 文档并没有真正解释 PropertyType When overridd
我有一个简单的 Windows 窗体应用程序,它使用 DataGridView绑定(bind)到 BindingList<>我的自定义对象 (MCO)。 我还实现了一个自定义 TypeDescript
如果我有: class A { public virtual string Owner { get; set; } } class B : A { public override st
我对这个方法的细节很感兴趣。如何以及通过什么来跟踪属性(property)的值(value)变化? 最佳答案 Andrew Smith 有一个 cool blog post关于它,我认为它会对您有所帮
我是 WPF 的新手,我正在尝试自定义一些数据网格以以可读的方式显示数据。我看过this article ,我想知道 - 最后将 PropertyDescriptor 属性转换为两个类,使用 as 关
是否有用于为属性设置默认 PropertyDescriptor 的属性?我只想给属性添加一个属性并指定 PropertyDescriptor。然后将创建 PropertyDescriptor 的实例。
我正在尝试在 WCF 服务中进行一些验证,为此我正在使用 WCFDataAnnotations我通过 this post 找到的 问题是它不能递归验证,所以对于嵌套对象它不起作用。这么说吧 [Data
我正在通过 获取有关类(class)的信息 Introspector.getBeanInfo(this.getClass()).getPropertyDescriptors() 然后通过调用 prop
我正在尝试检查一个属性是否应用了 DataMemberAttribute(使用 TypeDescriptor) 这是我现在拥有的: PropertyDescriptor targetProp = ta
我一直在努力让自定义 PropertyDescriptors 以我想要的方式使用 PropertyGrid。 前提: 我有一个名为“Animal”的类,其中包含属性 Age , Type , Loca
使用为 IsReadOnly() 方法返回值的 PropertyDescriptor 和与 ReadOnlyAttribute 关联的有什么区别>? 最佳答案 主要区别在于,如果您提供自己的 Prop
PropertyDescriptor是自定义属性描述符可以从中继承的抽象类。该类有一个 IsBrowsable 属性,根据 MSDN: Gets a value indicating whether
我有一些代码可以枚举一个对象并根据它的 ValidationAttribute 记录它的任何错误。 当它找到它们时,我希望创建一个名为 RuleViolations 的自定义类的集合。 RuleVio
我正在实现自定义 ModelBinder,我试图用 PropertyDescriptor.SetValue 设置一个属性我不明白为什么它不起作用。 对于某些复杂属性,未设置值但不会引发异常。该属性仍然
各位 SO 成员大家好! 我正在为 Eclipse RCP 应用程序编写一个插件,当我从 TreeViewer 选择一行时,它的属性会出现在属性 View 中。 最初我在寻找一种可以将某些属性设置为只
我是一名优秀的程序员,十分优秀!