WPF:带有列/行边距/填充的网格?
是否可以轻松地为 WPF 网格中的行或列指定边距和/或填充?
我当然可以添加额外的列来分隔事物,但这似乎是填充/边距的工作(它将使 XAML 变得更加简单)。有人从标准网格派生来添加此功能吗?
Is it easily possible to specify a margin and/or padding for rows or columns in a WPF Grid?
I could of course add extra columns to space things out, but this seems like a job for padding/margins (it will give much simplier XAML). Has someone derived from the standard Grid to add this functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
RowDefinition
和ColumnDefinition
的类型为ContentElement
,而Margin
严格来说是一个FrameworkElement
属性。所以对于你的问题,“这很容易实现吗”,答案是肯定的。不,我还没有看到任何布局面板展示这种功能。您可以按照建议添加额外的行或列。但您也可以在
Grid
元素本身或Grid
内的任何内容上设置边距,因此这是目前最好的解决方法。RowDefinition
andColumnDefinition
are of typeContentElement
, andMargin
is strictly aFrameworkElement
property. So to your question, "is it easily possible" the answer is a most definite no. And no, I have not seen any layout panels that demonstrate this kind of functionality.You can add extra rows or columns as you suggested. But you can also set margins on a
Grid
element itself, or anything that would go inside aGrid
, so that's your best workaround for now.在单元格控件外部使用
Border
控件并为其定义填充:来源:
Use a
Border
control outside the cell control and define the padding for that:Source:
你可以使用这样的东西:
或者,如果您不需要 TemplateBindings:
You could use something like this:
Or if you don't need the TemplateBindings:
我想我会添加自己的解决方案,因为还没有人提到这一点。您可以使用样式声明来定位网格中包含的控件,而不是基于网格设计用户控件。负责向所有元素添加填充/边距,而无需为每个元素进行定义,这既麻烦又费力。例如,如果您的网格只包含文本块,则可以执行以下操作:
这相当于“单元格填充” ”。
Thought I'd add my own solution because nobody yet mentioned this. Instead of designing a UserControl based on Grid, you can target controls contained in grid with a style declaration. Takes care of adding padding/margin to all elements without having to define for each, which is cumbersome and labor-intensive.For instance, if your Grid contains nothing but TextBlocks, you can do this:
Which is like the equivalent of "cell padding".
我很惊讶我还没有看到这个解决方案。
来自网络的框架(例如 bootstrap)将使用负边距来拉回行/列。
它可能有点冗长(尽管不是那么糟糕),但它确实有效,并且元素的间距和大小均匀。
在下面的示例中,我使用 StackPanel 根来演示如何使用边距均匀分布 3 个按钮。您可以使用其他元素,只需将内部 x:Type 从按钮更改为您的元素即可。
这个想法很简单,使用外部网格将元素的边距拉出其边界,拉动内部网格数量的一半(使用负边距),使用内部网格将元素均匀地间隔到您想要的数量。
更新:
一些用户的评论说它不起作用,这里有一个快速视频演示:https://youtu.be/rPx2OdtSOYI< /a>
I am surprised I did not see this solution posted yet.
Coming from the web, frameworks like bootstrap will use a negative margin to pull back rows / columns.
It might be a little verbose (albeit not that bad), it does work and the elements are evenly spaced and sized.
In the example below I use a
StackPanel
root to demonstrate how the 3 buttons are evenly spaced using margins. You could use other elements, just change the inner x:Type from button to your element.The idea is simple, use a grid on the outside to pull the margins of elements out of their bounds by half the amount of the inner grid (using negative margins), use the inner grid to evenly space the elements with the amount you want.
Update:
Some comment from a user said it doesn't work, here's a quick video demonstrating: https://youtu.be/rPx2OdtSOYI
编辑:
要为任何控件提供边距,您可以像这样用边框包裹控件
Edited:
To give margin to any control you could wrap the control with border like this
我最近在两列网格中遇到了类似的问题,我只需要右列元素的边距。两列中的所有元素都是 TextBlock 类型。
I had similar problem recently in two column grid, I needed a margin on elements in right column only. All elements in both columns were of type TextBlock.
我现在用我的一个网格做到了这一点。
I did it right now with one of my grids.
我最近在开发一些软件时遇到了这个问题,我突然想问为什么?他们为什么要这么做……答案就在我面前。一行数据就是一个对象,所以如果我们保持面向对象,那么特定行的设计就应该分开(假设以后需要重新使用行显示)。因此,我开始使用数据绑定堆栈面板和自定义控件来显示大多数数据。列表偶尔出现,但大多数情况下网格仅用于主页面组织(标题、菜单区域、内容区域、其他区域)。您的自定义对象可以轻松管理堆栈面板或网格中每行的任何间距要求(单个网格单元可以包含整个行对象。这还有一个额外的好处,可以对方向、展开/折叠等变化做出正确反应。
或者
如果您使用数据绑定,您的自定义控件也将继承 DataContext...我个人最喜欢这种方法的好处。
I ran into this problem while developing some software recently and it occured to me to ask WHY? Why have they done this...the answer was right there in front of me. A row of data is an object, so if we maintain object orientation, then the design for a particular row should be seperated (suppose you need to re-use the row display later on in the future). So I started using databound stack panels and custom controls for most data displays. Lists have made the occasional appearance but mostly the grid has been used only for primary page organization (Header, Menu Area, Content Area, Other Areas). Your custom objects can easily manage any spacing requirements for each row within the stack panel or grid (a single grid cell can contain the entire row object. This also has the added benefit of reacting properly to changes in orientation, expand/collapses, etc.
or
Your Custom controls will also inherit the DataContext if your using data binding...my personal favorite benefit of this approach.
您可以编写自己的从
Grid
继承的GridWithMargin
类,并重写ArrangeOverride
方法来应用边距You could write your own
GridWithMargin
class, inherited fromGrid
, and override theArrangeOverride
method to apply the margins一种可能性是添加固定宽度的行和列来充当您正在寻找的填充/边距。
您可能还认为您受到容器大小的限制,并且网格将变得与包含元素或其指定的宽度和高度一样大。您可以简单地使用没有设置宽度或高度的列和行。这样他们就会默认均匀地分割网格内的总空间。然后,只需将元素在网格内垂直和水平居中即可。
另一种方法可能是将所有网格元素包装在一个固定的单行和行中。具有固定大小和边距的列网格。您的网格包含固定宽度/高度的框,其中包含您的实际元素。
One possibility would be to add fixed width rows and columns to act as the padding / margin you are looking for.
You might also consider that you are constrained by the size of your container, and that a grid will become as large as the containing element or its specified width and height. You could simply use columns and rows with no width or height set. That way they default to evenly breaking up the total space within the grid. Then it would just be a mater of centering your elements vertically and horizontally within you grid.
Another method might be to wrap all grid elements in a fixed with single row & column grid that has a fixed size and margin. That your grid contains fixed width / height boxes which contain your actual elements.
虽然您无法向网格添加边距或填充,但您可以使用诸如框架(或类似容器)之类的东西,您可以将其应用到。
这样(如果您在单击按钮时显示或隐藏控件),您就不需要在可能与之交互的每个控件上添加边距。
将其视为将控件组隔离为单元,然后将样式应用于这些单元。
Though you can't add margin or padding to a Grid, you could use something like a Frame (or similar container), that you can apply it to.
That way (if you show or hide the control on a button click say), you won't need to add margin on every control that may interact with it.
Think of it as isolating the groups of controls into units, then applying style to those units.
在uwp中(Windows10FallCreatorsUpdate版本及以上)
in uwp (Windows10FallCreatorsUpdate version and above)
如前所述,创建一个 GridWithMargins 类。
这是我的工作代码示例
用法:
As was stated before create a GridWithMargins class.
Here is my working code example
Usage:
有时简单的方法是最好的。只需用空格填充字符串即可。如果只是几个文本框等,这是迄今为止最简单的方法。
您还可以简单地插入具有固定大小的空白列/行。非常简单,您可以轻松更改它。
Sometimes the simple method is the best. Just pad your strings with spaces. If it is only a few textboxes etc this is by far the simplest method.
You can also simply insert blank columns/rows with a fixed size. Extremely simple and you can easily change it.