用户控件对接属性
我正在尝试制作自己的用户控件,并且几乎已经完成,只是想添加一些润色。 我希望设计器中的选项“停靠在父容器中”。 有谁知道如何做到这一点,我找不到例子。 我认为这与对接属性有关。
I am trying to make my own user control and have almost finished it, just trying to add some polish. I would like the option in the designer to "Dock in parent container". Does anyone know how to do this I can't find an example. I think it has something to do with a Docking Attribute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还建议查看 对接属性。
这还会在控件的右上角显示“操作箭头”。
此选项早在 .NET 2.0 中就可用,如果您要查找的只是“在父容器中停靠/取消停靠”功能,则该选项要简单得多。 在这种情况下,Designer 类就显得大材小用了。
它还提供了
DockingBehavior.Never
和DockingBehavior.AutoDock
选项。Never
不显示箭头并以其默认的Dock
行为加载新控件,而AutoDock
显示箭头但自动将控件停靠为填充
。I would also suggest looking at the DockingAttribute.
This also displays the 'action arrow' on the upper right corner of the control.
This option is available as far back as .NET 2.0, and it is far simpler if all you're looking for is the 'Dock/Undock in Parent Container' function. The Designer classes are massive overkill in that case.
It also gives the options of
DockingBehavior.Never
andDockingBehavior.AutoDock
.Never
does not display the arrow and loads the new control at it's defaultDock
behavior, whileAutoDock
displays the arrow but automatically docks the control in asFill
.为了实现这一点,您需要实现几个类; 首先,您需要一个自定义 ControlDesigner然后您将需要一个自定义的 DesignerActionList。 两者都相当简单。
ControlDesigner:
DesignerActionList:
最后,您需要将设计器附加到您的控件:
简要说明
该控件有一个与其关联的
Designer
属性,该属性指出我们的自定义设计师。 该设计器中唯一的自定义是公开的DesignerActionList
。 它创建我们的自定义操作列表的实例并将其添加到公开的操作列表集合中。自定义操作列表包含一个
bool
属性 (DockInParent
),并为该属性创建一个操作项。 如果正在编辑的组件的Dock
属性为DockStyle.Fill
,则该属性本身将返回true
,否则返回false
,如果DockInParent
设置为true
,则组件的Dock
属性设置为DockStyle.Fill
,否则DockStyle.None
。这将在设计器中靠近控件右上角的位置显示一个小“操作箭头”,单击该箭头将弹出任务菜单。
I order to achieve this you will need to implement a couple of classes; first you will need a custom ControlDesigner and then you will need a custom DesignerActionList. Both are fairly simple.
The ControlDesigner:
The DesignerActionList:
Finally, you will need to attach the designer to your control:
Brief explanation
The control has a
Designer
attribute associated with it, which points out our custom designer. The only customization in that designer is theDesignerActionList
that is exposed. It creates an instance of our custom action list and adds it to the action list collection that is exposed.The custom action list contains a
bool
property (DockInParent
), and creates an action item for that property. The property itself will returntrue
if theDock
property of the component being edited isDockStyle.Fill
, otherwisefalse
, and ifDockInParent
is set totrue
, theDock
property of the component is set toDockStyle.Fill
, otherwiseDockStyle.None
.This will display the little "action arrow" close to the upper right corner of the control in the designer, and clicking the arrow will pop up the task menu.
如果您的控件继承自
UserControl
(或大多数其他可用控件),则只需将Dock
属性设置为DockStyle.Fill
。If your control inherits from
UserControl
(or most other controls available), you just need to set theDock
property toDockStyle.Fill
.