如何将不隐藏控件的自定义 Visible 属性 添加到 PropertyGrid?

How to add custom Visible property that doesn't hide the control to PropertyGrid?

我正在编写一个程序,用户可以在表单中编辑控件属性。要更改控件(文本框、标签等)属性,我正在使用 PropertyGrid。我想添加自定义 Visible 属性 ,当它在运行时变为 False 时不会隐藏控件。仅在保存更改时更改可见性。

我正在使用 的解决方案来显示控件的特定属性,例如 {Text, BackColor, 前景色字体大小位置可见}等

private void CreateUIEditor(Control c)
{
    if (c is Label)
    {
        propertyGrid.SelectedObject = new CustomObjectWrapper(c, new List<string>() 
        { "Text", "BackColor", "ForeColor", "Font", "Visible"});
    }
    //...
}
public class CustomObjectWrapper : CustomTypeDescriptor
{
    public object WrappedObject { get; private set; }
    public List<string> BrowsableProperties { get; private set; }
    public CustomObjectWrapper(object o, List<string> pList)
        : base(TypeDescriptor.GetProvider(o).GetTypeDescriptor(o))
    {
        WrappedObject = o;
        BrowsableProperties = pList;
    }
    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);
    }
}

这里的基本思想是有一个阴影 属性 不属于原始对象,但显示在 PropertyGrid 中。这样的属性可以属于代理class本身。

以下代理 class 将隐藏原来的 Visible 属性,但是它显示一个 Visible 属性,您可以更改但不会更改原始对象的可见性:

代码如下:

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, List<string> pList)
        : base(TypeDescriptor.GetProvider(o).GetTypeDescriptor(o))
    {
        WrappedObject = o;
        BrowsableProperties = pList;
    }
    public override PropertyDescriptorCollection GetProperties()
    {
        return this.GetProperties(new Attribute[] { });
    }
    public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        var properties = base.GetProperties(attributes).Cast<PropertyDescriptor>()
             .Where(p => p.Name != "Visible")
             .Where(p => BrowsableProperties.Contains(p.Name))
             .Select(p => TypeDescriptor.CreateProperty(
                 WrappedObject.GetType(),
                 p,
                 p.Attributes.Cast<Attribute>().ToArray()))
             .ToList();
        if (BrowsableProperties.Contains("Visible"))
        {
            var p = TypeDescriptor.GetProperties(this, true)["Visible"];
            properties.Add(TypeDescriptor.CreateProperty(
                this.GetType(), p, new[] { BrowsableAttribute.Yes }));
        }
        return new PropertyDescriptorCollection(properties.ToArray());
    }
    public bool Visible { get; set; }
    public override object GetPropertyOwner(PropertyDescriptor pd)
    {
        if (pd == null)
            return base.GetPropertyOwner(pd);
        else if (pd.Name == "Visible")
            return this;
        else
            return WrappedObject;
    }
}