1 回答
TA贡献1811条经验 获得超4个赞
如果您试图避免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以及分配给编辑/添加/删除列和行的动词。
- 1 回答
- 0 关注
- 161 浏览
添加回答
举报