TableLayoutPanelDesiner
is internal and has dependencies to other internal classes and you can not inherit from it. Also, it's not a good idea to create an new control designer for TableLayoutPanel
from scratch because you will lose all of current designer functionalities.
A really good trick is finding the designer and manipulate designer at design-time. Look at the Do something! and the Back Color new items:
To do so, you can find the designer of your TableLayoutPanel
at design-time and manipulate it. A good point is in OnHandleCreated
method. You can get an instance of IDesignerHost
from Site
of the control and then get the designer.
Then you can get the current action list for the control and create a new action list containing all those action items and add some new action items to the list.
MyTableLayoutPanel
using System;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
public class MyTableLayoutPanel : TableLayoutPanel
{
private IDesignerHost designerHost;
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (DesignMode && Site != null)
{
designerHost = Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
if (designerHost != null)
{
var designer = (ControlDesigner)designerHost.GetDesigner(this);
if (designer != null)
{
var actions = designer.ActionLists[0];
designer.ActionLists.Clear();
designer.ActionLists.Add(
new MyTableLayoutPanelActionList(designer, actions));
}
}
}
}
}
MyTableLayoutPanelActionList
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows;
using System.Windows.Forms.Design;
public class MyTableLayoutPanelActionList : DesignerActionList
{
MyTableLayoutPanel control;
ControlDesigner designer;
DesignerActionList actionList;
public MyTableLayoutPanelActionList(ControlDesigner designer,
DesignerActionList actionList) : base(designer.Component)
{
this.designer = designer;
this.actionList = actionList;
control = (MyTableLayoutPanel)designer.Control;
}
public Color BackColor
{
get { return control.BackColor; }
set
{
TypeDescriptor.GetProperties(Component)["BackColor"]
.SetValue(Component, value);
}
}
private void DoSomething()
{
MessageBox.Show("My Custom Verb added!");
}
public override DesignerActionItemCollection GetSortedActionItems()
{
var items = new DesignerActionItemCollection();
foreach (DesignerActionItem item in actionList.GetSortedActionItems())
items.Add(item);
var category = "New Actions";
items.Add(new DesignerActionMethodItem(this, "DoSomething",
"Do something!", category, true));
items.Add(new DesignerActionPropertyItem("BackColor", "Back Color",
category));
return items;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…