我正在通过扩展 ScrollableControl
构建自定义控件。
问题是我的自定义控件充当容器 - 我可以将控件拖入其中:
我的问题是如何在扩展 ScrollableControl
下面是两个测试控件,一个扩展Control
,第二个ScrollableControl
public class ControlBasedControl : Control
{
protected override Size DefaultSize
{
get { return new Size(100, 100); }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.LightCoral, ClientRectangle);
}
}
public class ScrollableControlBasedControl : ScrollableControl
{
public ScrollableControlBasedControl()
{
AutoScrollMinSize = new Size(200, 200);
}
protected override Size DefaultSize
{
get { return new Size(100, 100); }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.LawnGreen, ClientRectangle);
}
}
最佳答案
您在设计时从 [Designer] 属性获得“行为像容器”行为。从 Reference Source 复制粘贴:
[
ComVisible(true),
ClassInterface(ClassInterfaceType.AutoDispatch),
Designer("System.Windows.Forms.Design.ScrollableControlDesigner, " + AssemblyRef.SystemDesign)
]
public class ScrollableControl : Control, IArrangedElement {
// etc...
}
完成工作的是 ScrollableControlDesigner。本身并没有做太多事情,而是派生自 ParentControlDesigner,该设计器允许控件充当子控件的父控件,并在设计时为其提供类似容器的行为。
修复很简单,你只需要使用你自己的 [Designer] 属性来选择另一个设计师。添加对 System.Design 的引用并使其看起来像这样:
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms.Design; // Add reference to System.Design
[Designer(typeof(ControlDesigner))]
public class ScrollableControlBasedControl : ScrollableControl {
// etc...
}
关于c# - 扩展可滚动控件的用户控件 - 禁用容器功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43230202/