首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将不隐藏控件的自定义可见属性添加到PropertyGrid?

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

Stack Overflow用户
提问于 2019-07-17 07:25:22
回答 1查看 333关注 0票数 1

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

我使用Hide some properties in PropertyGrid at run-time的解决方案来显示控件的特定属性,如{TextBackColorForeColorFontE 114大小E 215E 116位置E 217E 118可见E 219}等等。

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-21 20:45:13

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

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

下面是代码:

代码语言:javascript
复制
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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57070602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档