根据列表框中数据库中的记录动态添加行 mvvm mvvm light

发布于 2024-09-10 06:55:11 字数 102 浏览 2 评论 0原文

我有一个列表框,在其中显示数据库中的记录。它在同一列中一个接一个地出现。如何以编程方式将记录放置为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳絮泡泡 2024-09-17 06:55:11

如果记录是(或可以是)固定宽度,则很简单:只需将 ListBox.ItemsPanel 更改为 WrapPanel 并将 ListBox 宽度设置为项目宽度的 3 倍多一点:

<DataTemplate DataType="{x:Type my:CustomItem}">
  <Border Width="100" ...>
    ...
  </Border>
</DataTemplate>

...

<ListBox ... Width="320">
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel/>
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
  ...
</ListBox>

如果需要划分可用宽度三,并且您知道所有项目的高度完全相同(或者如果您同意所有行都是任何项目的最大高度),则有一个非常简单的解决方案,使用 UniformGrid 且未指定高度:

<ListBox ... >
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <UniformGrid Width="3" />
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
  ...
</ListBox>

如果您想要将可用宽度除以 3 以获得项目宽度,但在垂直方向上尽可能紧密地堆叠项目,这是可能的,但在纯 XAML 中很棘手。 (我不建议在纯 XAML 中执行此操作,但如果您想这样做,可以创建一个包含三个星形大小的列和一个固定宽度列的隐藏网格,然后使用绑定到第一个的 ActualWidth隐藏网格的列。)

一种更简单的方法是创建一个简单的自定义面板,其水平方向类似于 UniformGrid,但垂直方向类似于 StackPanel。这是一个非常简单的 Panel 子类:

  • MeasureOverride 将所需宽度计算为 3* 任何子项的最大宽度
  • MeasureOverride 将所需高度计算为 sum(max(每组 3 的高度))
  • ArrangeOverride 每行布置 3 个子项,每个 DesiredWidth/3 为上一行的右侧,下一行是当前行向下的 max(height)

显然,如果您设计一个自定义面板,您不会对“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:

<DataTemplate DataType="{x:Type my:CustomItem}">
  <Border Width="100" ...>
    ...
  </Border>
</DataTemplate>

...

<ListBox ... Width="320">
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel/>
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
  ...
</ListBox>

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:

<ListBox ... >
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <UniformGrid Width="3" />
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
  ...
</ListBox>

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:

  • MeasureOverride computes desired width as 3* max width of any child
  • MeasureOverride computes desired height as sum(max(height of each group of 3))
  • ArrangeOverride lays out children 3 per row, each DesiredWidth/3 to the right of the previous one, and the next row being max(height) down from the current row

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文