在运行时隐藏 PropertyGrid 中的一些属性

我正在做一个项目,允许用户自定义Control. 我有一个具有类似的控制形式LabelTextBoxButtonPropertyGrid控制。当在用户点击Label我显示的属性LabelProeprtyGrid其为使用以下代码中的所有工作罚款:

propertyGrid1.SelectedObject = SelectedControl;

但我只想显示一些属性,如BackColorFontForeColorText。是否可以隐藏属性,因为我不希望用户更改它或向他们显示?如果是,如何?


喵喔喔
浏览 513回答 2
2回答

莫回无

我相信您正在寻找自定义类型描述符。虽然另一个答案是共享有关Browsableattribute 和BrowsableAttributesof 的正确信息PropertyGrid,但我想说这不是该问题的正确实用解决方案。Browsable为现有控件类(如Label、等)设置属性或任何其他自定义属性是不切实际的Button。因为通过这种方式,op 需要覆盖这些类的所有属性并用合适的属性装饰它们。甚至最糟糕的是,并非所有的财产都是可覆盖的。实际的解决方案是什么?正如我之前提到的,我相信您正在寻找自定义类型描述符。您可以提供有关分配新对象TypeDescriptor或实现ICustomTypeDescriptor或派生自的对象的元数据CustomTypeDescriptor。例子例如,我在这里创建了一个CustomObjectWrapper派生类,该类CustomTypeDescriptor在构造函数中接受一个对象。通过这种方式,我可以简单地通过覆盖GetProperties.然后,我没有将其分配button1给PropertyGrid,而是将其包裹起来CustomObjectWrapper并分配CustomObjectWrapper给属性网格。这样它只显示过滤后的属性,而这些属性实际上来自button1.这是植入:using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;public class CustomObjectWrapper : CustomTypeDescriptor{&nbsp; &nbsp; public object WrappedObject { get; private set; }&nbsp; &nbsp; public List<string> BrowsableProperties { get; private set; }&nbsp; &nbsp; public CustomObjectWrapper(object o)&nbsp; &nbsp; &nbsp; &nbsp; :base(TypeDescriptor.GetProvider(o).GetTypeDescriptor(o))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; WrappedObject = o;&nbsp; &nbsp; &nbsp; &nbsp; BrowsableProperties = new List<string>() { "Text", "BackColor" };&nbsp; &nbsp; }&nbsp; &nbsp; public override PropertyDescriptorCollection GetProperties()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return this.GetProperties(new Attribute[] { });&nbsp; &nbsp; }&nbsp; &nbsp; public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var properties = base.GetProperties(attributes).Cast<PropertyDescriptor>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Where(p=>BrowsableProperties.Contains(p.Name))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Select(p => TypeDescriptor.CreateProperty(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WrappedObject.GetType(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p.Attributes.Cast<Attribute>().ToArray()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.ToArray();&nbsp; &nbsp; &nbsp; &nbsp; return new PropertyDescriptorCollection(properties);&nbsp; &nbsp; }}并作为用法:propertyGrid1.SelectedObject = new CustomObjectWrapper(button1);您可以简单地添加新的属性名BrowsableProperties的CustomObjectWrapper。这是公共财产。

梵蒂冈之花

更新请注意,这仅对隐藏属性有用(如果可以)。Reza Aghaei 的答案实际上是正确的答案。我将把它留在这里,因为它适用于另一种情况,当您只想在可以访问属性时隐藏它。原来的最简单的方法可能是使用[Browsable(false)]可浏览属性类指定是否应在“属性”窗口中显示属性或事件。[Browsable(false)]public int SecretSquirrels{&nbsp; get; set;}也正如Marc Gravell所指出的,还有PropertyGrid.BrowsableAttributes 属性获取或设置与属性网格附加到的对象关联的可浏览属性。
打开App,查看更多内容
随时随地看视频慕课网APP