C# 需要从.NET Winforms控件第二部分的PropertyGrid中隐藏仅设计器属性
这是以下问题的后续问题: 我已经用自己的实现替换了默认的TypeDescriptorFilterService(太酷了!),FilterProperties事件处理程序正在启动。我看到前面的问题如何帮助隐藏那些特定的TableLayoutPanel属性 现在,我对项目有一个更一般的要求,即隐藏某些属性。我当前的具体目标是隐藏子类Windows窗体对象的“(Name)”属性(我的.NET项目利用设计器在内部管理对象名称,不希望用户在某些条件下更改甚至看到该值)。为了隐藏Name属性(在属性网格上实际显示为C# 需要从.NET Winforms控件第二部分的PropertyGrid中隐藏仅设计器属性,c#,.net,winforms,windows-forms-designer,design-surface,C#,.net,Winforms,Windows Forms Designer,Design Surface,这是以下问题的后续问题: 我已经用自己的实现替换了默认的TypeDescriptorFilterService(太酷了!),FilterProperties事件处理程序正在启动。我看到前面的问题如何帮助隐藏那些特定的TableLayoutPanel属性 现在,我对项目有一个更一般的要求,即隐藏某些属性。我当前的具体目标是隐藏子类Windows窗体对象的“(Name)”属性(我的.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;
}
MyMyExtendedDesignerObjects.Form
继承自System.Windows.Forms.Form。虽然我的表单对象(MyExtendedDesignerObjects.Form
)也是一个IDesignerFilter,但我不知道如何/在何处连接其设计器的PreFilterProperties事件处理程序
我想,一旦我可以将这个预过滤逻辑连接起来,那么我就可以管理这个项目的所有属性网格属性可见性目标
谢谢你的阅读 Name属性是一个特殊属性,由设计器扩展程序提供程序添加。使用此方法无法隐藏它,您需要找到创建
Name
属性的扩展程序提供程序,并用另一个创建Name
属性的扩展程序提供程序替换它,该扩展程序提供程序具有Browsable(false)
但对于其余属性,您可以在postfilterproperty
之后隐藏它们,方法是使用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-隐藏名称
属性
在下面的示例中,在自定义设计器界面的onload
方法中,我找到了IExtenderProviderService
,然后删除了现有的NameExtenderProvider
和NameInheritedExtenderProvider
,并用自定义实现替换它们。自定义实现基本上是我使用system.design中的reflector提取的,只需将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);
}
}
Name属性是一个特殊属性,由designer extender提供程序添加。我认为你不能用这种方法处理它。但是对于其他属性,您可以在
postfilterproperty
之后隐藏它们,方法是使用Browsable(false)
装饰它们,您仍然是我的英雄。您对WinForms designer的理解如此之深,对.NET中的类型系统的掌握如此之深,我感到难以置信。非常感谢。刚刚完成完全集成:它工作得很好,我一路上学到了很多东西。非常感谢你。