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

时间:2019-07-17 07:25:22

标签: c# winforms propertygrid

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

我正在使用Hide some properties in PropertyGrid的解决方案来显示控件的特定属性,例如{ Text BackColor ForeColor 字体大小位置可见}等

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);
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

enter image description here

代码如下:

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;
    }
}