如何将第三方控件嵌入ToolStrip控件,并提供Design-Time支持
ToolStripControlHost 旨在通过使用 ToolStripControlHost 构造函数或扩展 ToolStripControlHost 本身来启用任意 Windows 窗体控件的承载。通过扩展 ToolStripControlHost 并实现公开控件的常用属性和方法的属性和方法,可以更轻松地包装控件。还可以在 ToolStripControlHost 级别公开控件的事件。
通过派生在 ToolStripControlHost 中承载控件
-
扩展 ToolStripControlHost。实现一个无参数构造函数,该构造函数调用传入所需控件的基类构造函数。
// Call the base constructor passing in a MonthCalendar instance. public ToolStripMonthCalendar() : base (new MonthCalendar()) { }
-
声明与包装控件类型相同的属性,并在属性的访问器中返回为正确类型的控件。
Control
public MonthCalendar MonthCalendarControl { get { return Control as MonthCalendar; } }
-
使用扩展类中的属性和方法公开包装控件的其他常用属性和方法。
// Expose the MonthCalendar.FirstDayOfWeek as a property. public Day FirstDayOfWeek { get { return MonthCalendarControl.FirstDayOfWeek; } set { MonthCalendarControl.FirstDayOfWeek = value; } } // Expose the AddBoldedDate method. public void AddBoldedDate(DateTime dateToBold) { MonthCalendarControl.AddBoldedDate(dateToBold); }
-
(可选)重写 OnSubscribeControlEvents 和 OnUnsubscribeControlEvents 方法,并添加要公开的控件事件。
protected override void OnSubscribeControlEvents(Control c) { // Call the base so the base events are connected. base.OnSubscribeControlEvents(c); // Cast the control to a MonthCalendar control. MonthCalendar monthCalendarControl = (MonthCalendar) c; // Add the event. monthCalendarControl.DateChanged += new DateRangeEventHandler(OnDateChanged); } protected override void OnUnsubscribeControlEvents(Control c) { // Call the base method so the basic events are unsubscribed. base.OnUnsubscribeControlEvents(c); // Cast the control to a MonthCalendar control. MonthCalendar monthCalendarControl = (MonthCalendar) c; // Remove the event. monthCalendarControl.DateChanged -= new DateRangeEventHandler(OnDateChanged); }
-
为要公开的事件提供必要的包装。
// Declare the DateChanged event. public event DateRangeEventHandler DateChanged; // Raise the DateChanged event. private void OnDateChanged(object sender, DateRangeEventArgs e) { if (DateChanged != null) { DateChanged(this, e); } }
例
//Declare a class that inherits from ToolStripControlHost.
public class ToolStripMonthCalendar : ToolStripControlHost
{
// Call the base constructor passing in a MonthCalendar instance.
public ToolStripMonthCalendar() : base (new MonthCalendar()) { }
public MonthCalendar MonthCalendarControl
{
get
{
return Control as MonthCalendar;
}
}
// Expose the MonthCalendar.FirstDayOfWeek as a property.
public Day FirstDayOfWeek
{
get
{
return MonthCalendarControl.FirstDayOfWeek;
}
set { MonthCalendarControl.FirstDayOfWeek = value; }
}
// Expose the AddBoldedDate method.
public void AddBoldedDate(DateTime dateToBold)
{
MonthCalendarControl.AddBoldedDate(dateToBold);
}
// Subscribe and unsubscribe the control events you wish to expose.
protected override void OnSubscribeControlEvents(Control c)
{
// Call the base so the base events are connected.
base.OnSubscribeControlEvents(c);
// Cast the control to a MonthCalendar control.
MonthCalendar monthCalendarControl = (MonthCalendar) c;
// Add the event.
monthCalendarControl.DateChanged +=
new DateRangeEventHandler(OnDateChanged);
}
protected override void OnUnsubscribeControlEvents(Control c)
{
// Call the base method so the basic events are unsubscribed.
base.OnUnsubscribeControlEvents(c);
// Cast the control to a MonthCalendar control.
MonthCalendar monthCalendarControl = (MonthCalendar) c;
// Remove the event.
monthCalendarControl.DateChanged -=
new DateRangeEventHandler(OnDateChanged);
}
// Declare the DateChanged event.
public event DateRangeEventHandler DateChanged;
// Raise the DateChanged event.
private void OnDateChanged(object sender, DateRangeEventArgs e)
{
if (DateChanged != null)
{
DateChanged(this, e);
}
}
}
编译代码
此示例需要:
- 对 System 和 System.Windows.Forms 程序集的引用。