根据列表框中数据库中的记录动态添加行 mvvm mvvm light
我有一个列表框,在其中显示数据库中的记录。它在同一列中一个接一个地出现。如何以编程方式将记录放置为 3 行 3 列,即一行 3 条记录,之后一行 3 条记录。请建议?
谢谢。
I have a listbox in which I am displaying the records from the database. Its coming one after the other in a single column. How to put the records programitically as 3 rows and 3 columns i.e. 3 records in a row, after that 3 records in a row. Kindly suggest?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果记录是(或可以是)固定宽度,则很简单:只需将 ListBox.ItemsPanel 更改为 WrapPanel 并将 ListBox 宽度设置为项目宽度的 3 倍多一点:
如果需要划分可用宽度三,并且您知道所有项目的高度完全相同(或者如果您同意所有行都是任何项目的最大高度),则有一个非常简单的解决方案,使用 UniformGrid 且未指定高度:
如果您想要将可用宽度除以 3 以获得项目宽度,但在垂直方向上尽可能紧密地堆叠项目,这是可能的,但在纯 XAML 中很棘手。 (我不建议在纯 XAML 中执行此操作,但如果您想这样做,可以创建一个包含三个星形大小的列和一个固定宽度列的隐藏网格,然后使用绑定到第一个的 ActualWidth隐藏网格的列。)
一种更简单的方法是创建一个简单的自定义面板,其水平方向类似于 UniformGrid,但垂直方向类似于 StackPanel。这是一个非常简单的 Panel 子类:
显然,如果您设计一个自定义面板,您不会对“3”进行硬编码 - 您将使其成为一个属性,可能是一个 DependencyProperty ,这样您就可以轻松更改它,并且可以在需要时在其他地方重复使用您的面板。
If the records are (or can be) a fixed width, it is easy: Just change your ListBox.ItemsPanel to a WrapPanel and set the ListBox width to be just over 3x the width of the items:
If you need to divide the available width by three and you know that all of your items are exactly the same height (or if you are ok with all your rows being the max height of any item), there is a very simple solution using UniformGrid with no Height specified:
If you want to divide the available width by 3 to get the item width but stack the items as closely as possible vertically, it is possible but tricky in pure XAML. (I wouldn't recommend doing it in pure XAML but if you want to do it that way, you can create a hidden Grid with three star-sized columns and a fixed-width column, then use a binding to the ActualWidth of the first column of the hidden grid.)
An easier way is to create a simple custom panel that acts like UniformGrid horizontally but StackPanel vertically. This is a very simple Panel subclass:
Obviously if you design a custom panel you wouldn't hard-code the "3" - you would make it a property, probably a DependencyProperty, so you can easily change it and so that you can reuse your panel elsewhere if need be.