Hide some properties in PropertyGrid at run-time

耗尽温柔 提交于 2019-11-28 08:25:25

问题


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?


回答1:


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.




回答2:


UPDATE

Please note this is only useful for Hiding properties (when you can). Reza Aghaei answer is actually the correct answer.

Ill leave this here as its suitable for the other case when you just simply want to hide a property when you have access to it.

Original

Easiest way is probably to use

[Browsable(false)]

BrowsableAttribute Class

Specifies whether a property or event should be displayed in a Properties window.

[Browsable(false)]
public int SecretSquirrels
{
  get; set;
}

Also as pointed out by Marc Gravell, there is also

PropertyGrid.BrowsableAttributes Property

Gets or sets the browsable attributes associated with the object that the property grid is attached to.



来源:https://stackoverflow.com/questions/51611870/hide-some-properties-in-propertygrid-at-run-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!