wpf智能网格布局
我目前正在开发一个包含一个窗口的项目,该窗口需要显示 1 到 9 个用户控件。
我正在寻找最佳方法来确保所选用户控件的数量使用全部可用空间。
例如,如果选择了 9 个项目,则显示 3 x 3 的网格。但是,如果选择 8,则将显示 2 行,每行 3 个,下面有一行 2。 (使用 colspan)。
我一直在尝试使用统一的网格,因为这看起来好像大多数所需的行为已经存在。 (显示按顺序添加的项目,自动调整大小到窗口大小)。然而,当所选项目的数量不能被 3 整除时,我正在努力实现某种形式的智能!
以前有人遇到过这个问题吗?或者可以建议一个合适的方法来解决问题?
谢谢!!
I am currently working on a project which contains a window, that has a need to display between 1 and 9 user controls.
I am looking for the best way to ensure that the full space available is used by the number of user controls selected.
e.g. if 9 items are selected then a grid of 3 x 3 is displayed. However if 8 are selected then 2 rows of 3 would be shown with a row of 2 below. (using a colspan).
I have been trying to use a uniform grid as this looks as though most of the desired behaviour already exists. (displays items in the ordered added, auto resizes to window size). However I'm struggling to implement some form of intelligence when the number of items selected is not divisible by 3!
Has anybody encountered this problem before? Or can suggest a suitable way to solve the problem?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
WrapPanel
并将每个对象的Width
设置为环绕面板大小的 1/3。当第一行填满时,这会自动将项目移动到新行
Try using a
WrapPanel
and setting theWidth
of each object to 1/3 of the wrap panel's size.This will automatically move items to a new row when the first row fills up
坚持统一的网格。因为听起来您想要 3 列,所以无论显示的控件数量如何,您只需使用:
它会执行您所要求的操作 - 如果您有 8 个控件,您将在下面获得 2x3 + 2 额外的值。
Stick with the uniform grid. Since it sounds like you want 3 columns, regardless of the number of controls displayed, you just have to use:
And it will do what you've asked for -- if you have 8 controls, you'll get 2x3 + 2 extra below.