在运行时在PropertyGrid中隐藏一些属性 [英] Hide some properties in PropertyGrid at run-time
问题描述
我正在做一个允许用户自定义 Control
属性的项目。我有一个窗体,该窗体具有一个控件,如 Label
, TextBox
, Button
和 PropertyGrid
控件。当用户单击 Label
时,我在 ProeprtyGrid中显示
使用下面的代码都可以正常工作: Label
的属性
propertyGrid1.SelectedObject = SelectedControl;
但是我只想显示一些属性,例如 BackColor
,字体
, ForeColor
,文本
。是否可以隐藏属性,因为我不想让用户对其进行更改或显示?如果是,怎么办?
我相信您正在寻找自定义类型描述符。
另一个答案是共享有关 Browsable
属性和 BrowsableAttributes
的正确信息> PropertyGrid ,但我想说这不是解决该问题的合适方法。
设置可浏览
属性,或现有控件类的任何其他自定义属性,例如 Label
, Button
, 等等。因为以这种方式,op需要覆盖那些类的所有属性,并使用合适的属性来装饰它们。甚至最坏的是,并非所有财产都能被覆盖。
什么是实际的解决方案?
<如前所述,我相信您正在寻找自定义类型描述符。您可以提供有关对象的元数据,该对象分配新的
TypeDescriptor
或实现 ICustomTypeDescriptor
或从 CustomTypeDescriptor派生
。 示例
在这里,例如,我创建了一个 CustomTypeDescriptor
派生的> CustomObjectWrapper 类,该类在构造函数中接受对象。这样,我可以通过覆盖 GetProperties
来简单地过滤包装对象的属性。
然后,而不是分配 button1
到 PropertyGrid
,我将其包装在 CustomObjectWrapper
中并配置 CustomObjectWrapper
到属性网格。这样,它仅显示过滤后的属性,而这些属性实际上来自 button1
。
这里是植入:
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Linq;
公共类CustomObjectWrapper:CustomTypeDescriptor
{
公共对象WrappedObject {get;私人套装; }
公共列表< string> BrowsableProperties {get;私人套装; }
public CustomObjectWrapper(object o)
:base(TypeDescriptor.GetProvider(o).GetTypeDescriptor(o))
{
WrappedObject = o;
BrowsableProperties = new List< string>(){ Text, BackColor};
}
公共重写PropertyDescriptorCollection GetProperties()
{
返回this.GetProperties(new Attribute [] {});
}
公共重写PropertyDescriptorCollection GetProperties(Attribute []属性)
{
var properties = base.GetProperties(attributes).Cast< PropertyDescriptor>()
.Where (p => BrowsableProperties.Contains(p.Name))
.Select(p => TypeDescriptor.CreateProperty(
WrappedObject.GetType(),
p,
p。 Attributes.Cast< Attribute>()。ToArray()))
.ToArray();
返回新的PropertyDescriptorCollection(properties);
}
}
以及用法:
propertyGrid1.SelectedObject = new CustomObjectWrapper(button1);
您只需将新的属性名称添加到 BrowsableProperties
CustomObjectWrapper
。这是公共财产。
I am doing a project that allows user to customized the properties of a Control
. I have a form that has a control like Label
, TextBox
, Button
and PropertyGrid
control. When the user clicks on the Label
i am showing the properties of the Label
in the ProeprtyGrid
which is all working fine using below code:
propertyGrid1.SelectedObject = SelectedControl;
But I just want to show some properties like BackColor
, Font
, ForeColor
, Text
. Is it possible to hide the properties since I don't want user to change it or show to them? If yes, how?
I believe you are looking for custom type descriptors.
While the other answer is sharing correct information about Browsable
attribute and BrowsableAttributes
of PropertyGrid
, but I'd say it's not a proper practical solution for the problem.
It's not practical to set Browsable
attribute, or any other custom attributes for existing control classes like Label
, Button
, and so on. Because in this way, the op needs to override all properties of those classes and decorate them with suitable attribute. And even worst, not all propertied are overridable.
What's the practical solution?
As I mentioned earlier, I believe you are looking for custom type descriptors. You can provide metadata about an object assigning a new TypeDescriptor
or implementing ICustomTypeDescriptor
or deriving from CustomTypeDescriptor
.
Example
Here for example, I create a CustomObjectWrapper
class deriving from CustomTypeDescriptor
which accepts an object in constructor. This way I can simply filter the properties of the wrapped object by overriding GetProperties
.
Then instead of assigning button1
to PropertyGrid
, I wrap it in CustomObjectWrapper
and assing the CustomObjectWrapper
to property grid. This way it just shows the filtered properties and the properties are actually come from button1
.
Here is the implantation:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
public class CustomObjectWrapper : CustomTypeDescriptor
{
public object WrappedObject { get; private set; }
public List<string> BrowsableProperties { get; private set; }
public CustomObjectWrapper(object o)
:base(TypeDescriptor.GetProvider(o).GetTypeDescriptor(o))
{
WrappedObject = o;
BrowsableProperties = new List<string>() { "Text", "BackColor" };
}
public override PropertyDescriptorCollection GetProperties()
{
return this.GetProperties(new Attribute[] { });
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
var properties = base.GetProperties(attributes).Cast<PropertyDescriptor>()
.Where(p=>BrowsableProperties.Contains(p.Name))
.Select(p => TypeDescriptor.CreateProperty(
WrappedObject.GetType(),
p,
p.Attributes.Cast<Attribute>().ToArray()))
.ToArray();
return new PropertyDescriptorCollection(properties);
}
}
And as usage:
propertyGrid1.SelectedObject = new CustomObjectWrapper(button1);
You can simply add new property names to BrowsableProperties
of the CustomObjectWrapper
. It's a public property.
这篇关于在运行时在PropertyGrid中隐藏一些属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!