Silverlight 2:希望可变数量的项目占据固定宽度
堆栈面板不合作。 我们有固定的宽度,以及在其中从左到右布局的可变数量的项目。
我们有一个项目控件,它使用堆栈面板对它们进行布局:
<ItemsControl x:Name="testItems"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Stacktest:ItemControl />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
但这并不能正确调整项目的大小。 无论有多少可用空间,它们的大小始终相同。 如果项目太多,它们会在右侧被切断,而不是调整大小以使它们全部适合。知道如何实现这一点吗? 如果项目数量恒定,我会使用网格,但事实并非如此。 通常为 1-4 项。
如果 ItemsPanelTemplate 可以是具有可变列数的网格,那就太好了。 但我不知道在 ItemsPanelTemplate 中是否可以实现这一点(或具有相同结果的内容)。
答案是编写一个特殊的面板子类,为所包含的项目分配相同的宽度吗?
The stackpanel is not co-operating. We have a fixed width, and a variable number of items to lay out left-to-right inside it.
We have a an items control that lays them out with a stack panel:
<ItemsControl x:Name="testItems"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Stacktest:ItemControl />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
But this doesn't size the items correctly. They are always the same size, regardless of how much space is available. If there are too many items they are cut off on the right, rather than sized so that they all fit in. Any idea how to accomplish this? I'd use a grid if the number of items was constant, but it isn't. It's typically 1-4 items.
It would be nice if the ItemsPanelTemplate could be a grid with a variable number of columns. But I don't know if that (or something with the same result) is possible in an ItemsPanelTemplate.
Is the answer to write a special subclass of panel that allocates equal width to contained items?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你想要的是 UniformGrid。
您可以指定它只有一行,并且它应该将内部的所有项目布局为具有相同的宽度。
这可能不完全是您正在寻找的,但这是我能想到的最接近的。
我不确定 Silverlight 工具包是否提供这样的组件,但我看过一些帖子展示了如何构建一个组件。
例如,Jeff Wilcox 的博客就有一个。
I think what you want is the UniformGrid.
You can indicate that it has one row, and it should layout all items inside to have the same width.
This may not exactly be what you are looking for, but that's the closest I can think of.
I'm not sure if the Silverlight Toolkit offers such a component, but I've seen posts that show how to build one.
Jeff Wilcox's blog for example has one.
我可能有点老派,但我喜欢自己做这种事情。
您可以在后端编写一些代码,以在空间中放置动态数量的项目(使用网格),并使它们具有动态宽度和均匀间隔。
这可能会按照您想要的方式工作,并且可以调整。
I'm probably a little old school, but I like doing this type of thing myself.
You can write a little code on the backend to drop a dynamic number of items in the space (with a grid), and make them a dynamic width and evenly spaced.
This would likely work the way you want, and be tweakable.