具有自定义 ItemsPanel 模板的 WPF ListBox 如何根据其项目的大小调整自身大小

发布于 2024-08-08 04:33:38 字数 1331 浏览 8 评论 0原文

这是的后续问题,已得到解答。

使用代码:

<ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled">
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel Orientation="Vertical"/>
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
</ListBox>

您可以创建一个列表,其中项目从左到右填充列中的 ListBox ,并且特定列中的最后一个项目永远不会被截断(这意味着如果该项目太高而无法容纳,它被移动到下一列的顶部)。我现在如何动态调整 ListBoxWidth 以“适合”它的项目并在水平方向上获得相同的行为。因此,假设如下:

如果在给定的 Height 处,只需要一个 Column 即可容纳 Width 的所有项目。 ListBox 应该是项目的Width 的大小。

或者,在给定的高度处,需要有四个,其大小需要为项目宽度的四倍。

最后,假设在给定的高度处,需要有比列表框允许的最大宽度更多的 ,它需要计算它应该有多宽,以便 Column 中无法容纳的项目不会被截断,而是在用户滚动到右侧之前不会显示。

感谢您的帮助!!

编辑:

我认为表达上述内容的一种更简单的方法是我正在寻找一种方法来避免“剪切”列表中的项目。我想创建一个在网格中显示项目的列表,但您不必滚动,因为某些项目被部分覆盖,并且需要您向右或向左滚动才能查看该项目的其余部分(尽管列表只能这么宽或这么高,如果有足够的项目,您仍然需要滚动才能看到所有项目,那么您看到的任何项目都应该完全显示)。而且我只关心列表的初始绘制。它不必“捕捉”项目以始终适合,但它应该能够滚动,以便它用它显示的项目完全填充可用空间。

在我看来,这样做的关键是找出运行时列表项的大小以及列表框中内容容器的大小,然后您可以在列表最初之前调整它以完全适合这么多项目画的?

谢谢!

This is a follow up question to this, which was answered.

Using the code:

<ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled">
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel Orientation="Vertical"/>
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
</ListBox>

You can create a list where the items populate the ListBox in columns from left to right and the last item in a particular column is never cutoff (meaning if the item was to tall to fit, it is moved to the top of the next column). How could I now dynamically adjust the Width of the ListBox to 'fit' it's items and get that same behavior horizontally. So suppose the following:

If, at a given Height, there only needed to be one Column to fit all of the items the Width of the ListBox should be the size of the Width of the items.

Or, at a given Height there needs to be four Columns, it would need to be size to four times the Width of the items.

Finally, say at a given Height there needs to be more Columns than the maximum allowed Width of theListBox, it would need to calculate how wide it should be so that the items in the Columns that would not be able to fit were not truncated but just not shown until the user scrolled to the right.

Thanks for your help!!

EDIT:

I think a simpler way to phrase the above is that I'm looking for a way to avoid 'clipping' items in the list. I'd like to create a list that displays items in grid but where you wouldn't have to scroll because some items were partially covered and would require you to scroll to the right or left to see the remainder of that item (though the list could only be so wide or tall and if there were enough items you'd still need to scroll to see all of them, just any items you saw should be fully displayed). Also I only care about the initial drawing of the list. It doesn't have to 'snap' items to always fit but it should be able to be scrolled so that it fills the available space completely with the items it does display.

It seemed to me the key do doing this is to figure out the size of a list item at runtime and the size of the content container in the listbox and then you could adjust it to completely fit so many items at once before the list is initially drawn?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

呢古 2024-08-15 04:33:38

这是一个相当复杂的问题。我不是 100% 确定您要在这里完成什么...您看到的列数是否需要随着高度的增加而增加或减少?我希望减少,但从你提出问题的方式来看,你似乎想要相反的结果,我正在努力理解这一点。

我认为在尝试理解这些问题时,对 WPF 布局系统的基本了解非常有用。另一件有用的事情是,某物的大小很大程度上取决于其布局容器,而不是物体本身。

在您的问题中,您询问如何使列表框根据内容动态调整大小。 ListBox 不是布局控件,但类似 ​​WrapPanel 的东西却是。 WrapPanel 的功能是说,即使Panel 的内容尺寸增加,宽度也保持不变。这与其他控件不同,例如网格。

这里有一些关于 WPF 布局系统的好链接。希望这有帮助。请回来对您的问题进行一些澄清,看看我们是否无法在您需要时为您提供更多帮助。

http://www.wpftutorial.net/LayoutProperties.html
http://msdn.microsoft.com/en-us/library/ms745058。 ASPX

This is a pretty complicated question. I'm not 100% sure what you are trying to accomplish here... does the number of columns you see need to increase or decrease as the height increases? I would hope decrease, but the way you worded your question it seems you want the opposite and I'm trying to understand this.

I think a basic understanding of WPF's layout system is really useful when trying to understand these questions. Another thing that is useful is that the size of something is largely determined by its layout container, rather than the thing itself.

In your question, you ask how to get your ListBox to dynamically size based on the contents. A ListBox is not a layout control, but something like a WrapPanel is. The function of a WrapPanel is to say at the same width, even if the contents of the Panel increases in size. This is not the same for other controls, like a Grid for example.

Here's a couple of good links about the layout system in WPF. Hopefully this helps. Come back with some clarification of your question and see if we can't get you some more help if you need it.

http://www.wpftutorial.net/LayoutProperties.html
http://msdn.microsoft.com/en-us/library/ms745058.aspx

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