Name 属性是一个特殊的属性,它是由设计器扩展提供者添加的。您无法使用此方法隐藏它,您需要找到创建该属性的扩展器提供程序并将其Name
替换为另一个创建该Name
属性的扩展程序提供程序Browsable(false)
。
但是对于其余的属性,您可以在 之后隐藏它们,方法PostFilterProperties
是用Browsable(false)
示例 1 - 隐藏Locked
和Tag
属性
请注意一点: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
,然后我删除了现有的NameExtenderProvider
并NameInheritedExtenderProvider
用我的自定义实现替换它们。自定义实现本质上是我使用 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);
}
}
