wpf - 如何对面板中的所有元素应用相同的边距?
有没有一种方法可以自动告诉面板(例如停靠面板)内的所有子项(例如标签、文本框等)的边距为 5?
即,与必须单独为每个元素设置边距相反 - 还要注意在面板本身上设置边距是不好的,因为面板有边距而不是元素。
顺便说一下 - 我注意到 DockPanel 上似乎没有 PADDING 元素(这会有帮助)
Is there a way of automatically tells all children items (e.g. labels, textboxes etc) to have a margin of 5, within a panel (e.g. dockpanel)?
i.e. as opposed to having to set the margin for each element separately - also noting setting the margin on the panel itself is no good as then the panel has the margin not the elements.
by the way - I note there doesn't seem to be a PADDING element on the DockPanel (which would have helped)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如这个答案 https://stackoverflow.com/a/21479885/10550394 中所述,您可以简单地定义一个样式您在坞站面板资源中的控件。这是一个简单的版本:
As outlined in this answer https://stackoverflow.com/a/21479885/10550394, you can simply define a style for your controls in the resources of your dockpanel. Here is a simple version:
我相信答案是“不”。边距不能像字体大小那样继承,因此您需要执行以下操作之一:
使用
Grid
而不是DockPanel
。这将允许您使用行和列定义来保持项目之间的间距一致。使用样式。您仍然需要引用每个元素的样式(例如,
Style="{StaticResource MarginStyle}"
,这将需要更多的输入,而不仅仅是Margin="10,5"
,但它允许您将所有边距值保留在一个位置。咬紧牙关并进行设置 。单独设置每个元素的边距。
I believe the answer is "no". Margin is not inheritable the way, say, font size is, so you would need to do one of the following:
Use a
Grid
instead of aDockPanel
. This would allow you to use row and column definitions to maintain consistent spacing between items.Use a style. You will still have to reference the style for each element (e.g.,
Style="{StaticResource MarginStyle}"
, which will require more typing than justMargin="10,5"
, but it would allow you to keep the margin values all in one place.Bite the bullet and set the margin of each element individually.