我正在设计一个简单的扩展器控件。
我已经从UserControl派生,绘制内部控件,构建,运行;一切都好。
由于内部控件是一个面板,所以我想在设计时将其用作容器。实际上,我已经使用了以下属性:
[Designer(typeof(ExpanderControlDesigner))]
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
我说太棒了。但它不是...
其结果是,我可以在设计时将其用作容器,但是:
我遗漏了什么?下面是完整的代码...为什么这段代码不能工作?
[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)。
发布于 2010-04-23 07:24:20
ParentControlDesigner不知道你想要做什么。它只知道你想让你的UserControl成为一个容器。
你需要做的是实现你自己的设计器,在面板上启用设计模式:
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace MyCtrlLib
{
// specify my custom designer
[Designer(typeof(MyCtrlLib.UserControlDesigner))]
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
// define a property called "DropZone"
[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Panel DropZone
{
get { return panel1; }
}
}
// my designer
public class UserControlDesigner : ParentControlDesigner
{
public override void Initialize(System.ComponentModel.IComponent component)
{
base.Initialize(component);
if (this.Control is UserControl1)
{
this.EnableDesignMode(
(UserControl1)this.Control).DropZone, "DropZone");
}
}
}
}
这是我在CodeProject上从Henry Minute上学到的。有关该技术的一些改进,请参阅链接。
发布于 2013-01-15 21:31:34
除了上面的答案之外。评论中提到,用户可以拖动WorkingArea。我的解决方法是将WorkingArea面板包含在另一个面板中,将其设置为Dock.Fill。为了禁止用户将其改回,我创建了一个类ContentPanel,它覆盖并隐藏了Dock属性:
class ContentPanel : Panel
{
[Browsable(false)]
public override DockStyle Dock
{
get { return base.Dock; }
set { base.Dock = DockStyle.Fill; }
}
}
对我来说,这就足够安全了。我们只是在内部使用控件,所以我们主要是想防止开发人员意外地拖来拖去。不管怎样,肯定有办法把事情搞砸。
发布于 2013-03-05 11:56:02
为了防止在设计器中移动工作区/调整其大小,您必须为该工作区创建一个类,该类向设计器隐藏位置、高度、宽度和大小属性:
public class WorkingArea : Panel
{
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new Point Location
{
get
{
return base.Location;
}
set
{
base.Location = value;
}
}
...
}
https://stackoverflow.com/questions/2694889
复制相似问题