.NET Control.Margin 属性有什么用?
我假设 C# 的 margin 属性具有与 CSS 类似的含义 - 控件外部的间距。 但无论我输入什么值,边距值似乎都会被忽略。
然后我读到了SDK:
设置 Margin 属性 停靠的控件对 控制距离 其容器的边缘。
鉴于我将控件放置在表单上,并且可能对接它们,Margin 属性会给我带来什么?
I assumed that the C# margin property had a meaning like in CSS - the spacing around the outside of the control. But Margin values seem to be ignored to matter what values I enter.
Then I read on the SDK:
Setting the Margin property on a
docked control has no effect on the
distance of the control from the the
edges of its container.
Given that I'm placing controls on forms, and perhaps docking them, what does the Margin property get me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不使用布局容器,而是手动放置控件,Control.Margin 属性在设计时也很有用。
它会影响手动拖动的控件之间的距离,对齐线出现。
例如,对于文本框的默认边距值为 3,您将具有以下对齐线:
对于边距 10 - 这些(在两种情况下标签的边距均为 3):
因此,如果您对 UI 有一些严格的指导原则,那么您只需根据需要设置边距并将控件拖动到对齐线即可。
Control.Margin property could be also useful at design time if you do not use layout container, but rather place controls manually.
It affects the distance between manually dragged controls at which snaplines appear.
E.g. for default margin value of 3 for text box you would have this snaplines:
And for margin of 10 - these (label has margin of 3 in both cases):
So if you have some strict guidelines for you UI then you just set the margins as you need and drag controls to the snaplines.
margin 属性由您的控制主机(例如,Panel)使用的任何布局引擎使用,以布局引擎认为合适的方式使用。 然而,正如您所假设的那样,它最好用于间距。 只需阅读该特定布局引擎的文档即可。
例如,当使用 FlowLayoutPanel 或 TableLayoutPanel 时,它可以非常方便 - 减少默认填充或稍微间隔一些。 显然,如果您编写自定义布局提供程序,则可以使用您认为合适的 Margin。
The margin property is used by whatever layout engine your control host (Panel, for example) is using, in whatever way that layout engine sees fit. However, it is best used for spacing just as you assume. Just read the documentation for that specific layout engine.
It can be very handy when using a FlowLayoutPanel or TableLayoutPanel, for example - to either reduce the default padding or space things out a bit. Obviously, if you write a custom layout provider, you can use Margin however you see fit.
正如 Philip Rieck 所说,边距属性仅受执行布局的容器控件的尊重。 下面的示例非常清楚地说明了
TableLayoutPanel
如何尊重 Margin 属性:我相信唯一尊重此属性的 .NET 2.0 内置控件是
FlowLayoutPanel
和TableLayoutPanel
; 希望第三方组件也尊重它。 其他场景基本没有影响。Like Philip Rieck said, the margin property is only respected by container controls that perform layout. Here's an example that makes it fairly clear how the
TableLayoutPanel
respects the Margin property:I believe the only .NET 2.0 built-in controls that respect this property are
FlowLayoutPanel
andTableLayoutPanel
; hopefully third-party components respect it as well. It has basically no effect in other scenarios.