如果你想避免TableLayoutPanelDesigner
自己写,那么我可以建议一个解决方法。
ITypeDescriptorFilterService
是负责调用PreFilterProperties
设计器方法的接口。该类DesignSurface
有一个在其ServiceContainer
. 因此,您可以删除现有的注册实例并注册您自己的ITypeDescriptorFilterService
.
在新的实现中,覆盖FilterProperties
并做任何你认为合适的事情,例如你可以检查组件的类型是否为TableLayoutPanel
,然后不要调用它的设计器PreFilterProperties
。
然后总结解决方案,您需要通过从DesignSurface
类派生并删除已注册ITypeDescriptorFilterService
并注册您创建的所需实例来创建自己的设计图面。
例子
仅供参考,您要查找的属性的名称是,默认情况下ColumnStyles
它具有Browsable(false)
属性。但是默认的设计TableLayoutPanel
器用可浏览的版本替换了这个属性。
我在这个例子中所做的是阻止设计者使这些属性可见。
首先提供一个自定义实现ITypeDescriptorFilterService
。下面的一个实际上是System.Design
装配中的现有实现,我已经改变了它的FilterProperties
方法并检查了组件是否是TableLayoutPanel
,我要求什么都不做。
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
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");
if (typeof(TableLayoutPanel).IsAssignableFrom(component.GetType()))
return true;
IDesigner designer = this.GetDesigner(component);
if (designer is IDesignerFilter)
{
((IDesignerFilter)designer).PreFilterProperties(properties);
((IDesignerFilter)designer).PostFilterProperties(properties);
}
return designer != null;
}
}
然后通过从以下派生来创建设计表面DesignSurface
:
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
public class MyDesignSurface : DesignSurface
{
public MyDesignSurface() : base()
{
this.ServiceContainer.RemoveService(typeof(ITypeDescriptorFilterService));
this.ServiceContainer.AddService(typeof(ITypeDescriptorFilterService), new TypeDescriptorFilterService());
}
}
然后例如以这种方式初始化设计表面:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var surface = new MyDesignSurface();
var host = (IDesignerHost)surface.GetService(typeof(IDesignerHost));
var selectionService = (ISelectionService)surface.GetService(typeof(ISelectionService));
surface.BeginLoad(typeof(Form));
var root = (Form)host.RootComponent;
var tableLayoutPanel1 = (Control)host.CreateComponent(typeof(TableLayoutPanel), "tableLayoutPanel1");
root.Controls.Add(tableLayoutPanel1);
var view = (Control)surface.View;
view.Dock = DockStyle.Fill;
this.Controls.Add(view);
selectionService.SetSelectedComponents(new[] { tableLayoutPanel1 });
var propertyGrid1 = new PropertyGrid() { Dock = DockStyle.Right, Width = 200 };
this.Controls.Add(propertyGrid1);
propertyGrid1.SelectedObjects = selectionService.GetSelectedComponents().Cast<object>().ToArray();
}
}
然后运行您的设计器应用程序,您将看到Columns
属性Rows
按预期隐藏。
您需要隐藏ColumnCount
和RowCount
属性以及分配给编辑/添加/删除列和行的动词。