【发布时间】:2010-04-22 22:10:12
【问题描述】:
我正在设计一个简单的扩展控件。
我从UserControl派生,绘制内部控件,构建,运行;没事。
由于内部控件是一个面板,我想在设计时将它用作容器。确实我已经使用了这些属性:
[Designer(typeof(ExpanderControlDesigner))]
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
我说太好了。但不是……
结果是我可以在设计时将其用作容器,但是:
- 添加的控件返回已嵌入用户控件的内部控件
- 即使我将在设计时添加的控件推到顶部,在运行时它又会返回到嵌入到用户控件的控件上
- 我无法在设计时将容器区域限制为面板区域
我错过了什么?这是完整的代码......为什么这个sn-p代码不起作用?
[Designer(typeof(ExpanderControlDesigner))]
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public partial class ExpanderControl : UserControl
{
public ExpanderControl()
{
InitializeComponent();
....
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
internal class ExpanderControlDesigner : ControlDesigner
{
private ExpanderControl MyControl;
public override void Initialize(IComponent component)
{
base.Initialize(component);
MyControl = (ExpanderControl)component;
// Hook up events
ISelectionService s = (ISelectionService)GetService(typeof(ISelectionService));
IComponentChangeService c = (IComponentChangeService)GetService(typeof(IComponentChangeService));
s.SelectionChanged += new EventHandler(OnSelectionChanged);
c.ComponentRemoving += new ComponentEventHandler(OnComponentRemoving);
}
private void OnSelectionChanged(object sender, System.EventArgs e)
{
}
private void OnComponentRemoving(object sender, ComponentEventArgs e)
{
}
protected override void Dispose(bool disposing)
{
ISelectionService s = (ISelectionService)GetService(typeof(ISelectionService));
IComponentChangeService c = (IComponentChangeService)GetService(typeof(IComponentChangeService));
// Unhook events
s.SelectionChanged -= new EventHandler(OnSelectionChanged);
c.ComponentRemoving -= new ComponentEventHandler(OnComponentRemoving);
base.Dispose(disposing);
}
public override System.ComponentModel.Design.DesignerVerbCollection Verbs
{
get
{
DesignerVerbCollection v = new DesignerVerbCollection();
v.Add(new DesignerVerb("&asd", new EventHandler(null)));
return v;
}
}
}
我找到了很多资源(Interaction、designed、limited area),但没有什么对操作有用...
实际上有一个技巧,因为 System.Windows.Forms 类可以被设计(像往常一样)并在运行时具有正确的行为(例如 TabControl)。
【问题讨论】:
标签: c# winforms user-controls