.NET Winforms控件第二部分需要从PropertyGrid隐藏仅设计师属性

时间:2019-11-13 16:56:06

标签: c# .net winforms windows-forms-designer design-surface

这是针对以下问题的后续问题:Need To Hide A Designer-Only Property From PropertyGrid For A .NET Winforms Control

我已经用我自己的实现替换了默认的TypeDescriptorFilterService(太酷了!),并且FilterProperties事件处理程序正在触发。我看到上一个问题如何帮助隐藏那些特定的TableLayoutPanel属性。

现在,我对项目有一个更一般的要求以隐藏某些属性。我当前的特定目标是隐藏我的子类Windows Form对象的“(名称)”属性(我的.NET项目利用设计器在内部管理对象名称,并且不希望用户在某些情况下更改甚至看到该值) )。为了隐藏Name属性(实际上在属性网格上显示为(Name)),我添加了以下代码:

  public bool FilterProperties(IComponent component, IDictionary properties)
  {
     if (component == null)
        throw new ArgumentNullException("component");
     if (properties == null)
        throw new ArgumentNullException("properties");
     if (typeof(MyExtendedDesignerObjects.Form).IsAssignableFrom(component.GetType()))
     {
        properties.Remove("Name");
        return true;
     }
     IDesigner designer = this.GetDesigner(component);
     if (designer is IDesignerFilter)
     {
        ((IDesignerFilter)designer).PreFilterProperties(properties);
        ((IDesignerFilter)designer).PostFilterProperties(properties);
     }
     return designer != null;
  }

我的MyExtendedDesignerObjects.Form继承自System.Windows.Forms.Form。虽然我的Form对象(MyExtendedDesignerObjects.Form)也是IDesignerFilter,但我不知道如何/在何处连接其设计器的PreFilterProperties事件处理程序。

我认为一旦可以连接此预过滤器逻辑,就可以管理该项目的所有属性网格属性可见性目标。

感谢您的阅读。

1 个答案:

答案 0 :(得分:1)

Name属性是一个特殊属性,由设计师扩展程序提供程序添加。您无法使用此方法隐藏它,您需要找到创建Name属性的扩展程序提供程序,并将其替换为另一个创建具有Name属性的Browsable(false)属性的扩展程序提供程序。

但是对于其他属性,可以通过用PostFilterProperties装饰

来将它们隐藏在Browsable(false)之后

示例1-隐藏LockedTag属性

请注意以下几点:Locked是设计时属性,而Tag是普通属性。

像这样覆盖FilterProperties方法:

bool ITypeDescriptorFilterService.FilterProperties(IComponent component, IDictionary properties)
{
    if (component == null)
        throw new ArgumentNullException("component");
    if (properties == null)
        throw new ArgumentNullException("properties");


    properties.Remove("Tag");


    IDesigner designer = this.GetDesigner(component);
    if (designer is IDesignerFilter)
    {
        ((IDesignerFilter)designer).PreFilterProperties(properties);
        ((IDesignerFilter)designer).PostFilterProperties(properties);

        var propertyName = "Locked";
        var attributeArray = new Attribute[] { BrowsableAttribute.No };
        var property = properties[propertyName];
        if (property != null)
            properties[propertyName] = TypeDescriptor.CreateProperty(typeof(IDesigner),
                (PropertyDescriptor)property, attributeArray);
    }

    return designer != null;
}

示例2-隐藏Name属性

在下面的示例中,在我的自定义设计器图面的OnLoaded方法中,我找到了IExtenderProviderService,然后删除了现有的NameExtenderProviderNameInheritedExtenderProvider并用我的自定义实现替换了它们。自定义实现本质上就是我使用system.design的反射器提取的内容,只需将Browsable属性更改为false:

using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;

public class TypeDescriptorFilterService : ITypeDescriptorFilterService
{
    internal TypeDescriptorFilterService()
    {
    }

    private IDesigner GetDesigner(IComponent component)
    {
        ISite site = component.Site;
        if (site != null)
        {
            IDesignerHost service = site.GetService(typeof(IDesignerHost)) as IDesignerHost;
            if (service != null)
                return service.GetDesigner(component);
        }
        return (IDesigner)null;
    }

    bool ITypeDescriptorFilterService.FilterAttributes(IComponent component, IDictionary attributes)
    {
        if (component == null)
            throw new ArgumentNullException("component");
        if (attributes == null)
            throw new ArgumentNullException("attributes");
        IDesigner designer = this.GetDesigner(component);
        if (designer is IDesignerFilter)
        {
            ((IDesignerFilter)designer).PreFilterAttributes(attributes);
            ((IDesignerFilter)designer).PostFilterAttributes(attributes);
        }
        return designer != null;
    }

    bool ITypeDescriptorFilterService.FilterEvents(IComponent component, IDictionary events)
    {
        if (component == null)
            throw new ArgumentNullException("component");
        if (events == null)
            throw new ArgumentNullException("events");
        IDesigner designer = this.GetDesigner(component);
        if (designer is IDesignerFilter)
        {
            ((IDesignerFilter)designer).PreFilterEvents(events);
            ((IDesignerFilter)designer).PostFilterEvents(events);
        }
        return designer != null;
    }

    bool ITypeDescriptorFilterService.FilterProperties(IComponent component, IDictionary properties)
    {
        if (component == null)
            throw new ArgumentNullException("component");
        if (properties == null)
            throw new ArgumentNullException("properties");

        IDesigner designer = this.GetDesigner(component);
        if (designer is IDesignerFilter)
        {
            ((IDesignerFilter)designer).PreFilterProperties(properties);
            ((IDesignerFilter)designer).PostFilterProperties(properties);
        }

        return designer != null;
    }
}

public class MyDesignSurface : DesignSurface
{
    public MyDesignSurface() : base()
    {
        this.ServiceContainer.RemoveService(typeof(ITypeDescriptorFilterService));
        this.ServiceContainer.AddService(typeof(ITypeDescriptorFilterService), new TypeDescriptorFilterService());
    }

    protected override void OnLoaded(LoadedEventArgs e)
    {
        base.OnLoaded(e);
        var svc = (IExtenderProviderService)this.ServiceContainer.GetService(typeof(IExtenderProviderService));
        var providers = (ArrayList)svc.GetType().GetField("_providers",
            System.Reflection.BindingFlags.NonPublic |
            System.Reflection.BindingFlags.Instance).GetValue(svc);
        foreach (IExtenderProvider p in providers.ToArray())
            if (p.ToString().Contains("NameExtenderProvider") ||
               p.ToString().Contains("NameInheritedExtenderProvider"))
                svc.RemoveExtenderProvider(p);

        svc.AddExtenderProvider(new NameExtenderProvider());
        svc.AddExtenderProvider(new NameInheritedExtenderProvider());
    }
}


[ProvideProperty("Name", typeof(IComponent))]
public class NameExtenderProvider : IExtenderProvider
{
    private IComponent baseComponent;

    internal NameExtenderProvider()
    {
    }

    protected IComponent GetBaseComponent(object o)
    {
        if (this.baseComponent == null)
        {
            ISite site = ((IComponent)o).Site;
            if (site != null)
            {
                IDesignerHost service = (IDesignerHost)site.GetService(typeof(IDesignerHost));
                if (service != null)
                    this.baseComponent = service.RootComponent;
            }
        }
        return this.baseComponent;
    }

    public virtual bool CanExtend(object o)
    {
        return this.GetBaseComponent(o) == o || TypeDescriptor.GetAttributes(o)[typeof(InheritanceAttribute)].Equals((object)InheritanceAttribute.NotInherited);
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [ParenthesizePropertyName(true)]
    [MergableProperty(false)]
    [Category("Design")]
    [Browsable(false)]
    public virtual string GetName(IComponent comp)
    {
        ISite site = comp.Site;
        if (site != null)
            return site.Name;
        return (string)null;
    }

    public void SetName(IComponent comp, string newName)
    {
        ISite site = comp.Site;
        if (site == null)
            return;
        site.Name = newName;
    }

    public class MyFormDesigner : DocumentDesigner
    {

    }
}

public class NameInheritedExtenderProvider : NameExtenderProvider
{
    internal NameInheritedExtenderProvider()
    {
    }
    public override bool CanExtend(object o)
    {
        return this.GetBaseComponent(o) != o && !TypeDescriptor.GetAttributes(o)[typeof(InheritanceAttribute)].Equals((object)InheritanceAttribute.NotInherited);
    }

    [ReadOnly(true)]
    public override string GetName(IComponent comp)
    {
        return base.GetName(comp);
    }
}

enter image description here