面板布局可均匀挤压项目以适应
我正在寻找一个像水平堆栈面板一样工作的 WPF 布局,这样当有足够的空间容纳项目时,项目会选择自己的理想宽度,但当项目不适合可用的面板空间时,还会均匀地挤压项目。
我可以通过这个接近我想要的:
<Grid Width="400" Height="80">
<UniformGrid Rows="1" HorizontalAlignment="Left">
<Button Content="Long Button" />
<Button Content="Very Long Button" />
<Button Content="Button" />
<Button Content="Button" />
<Button Content="Button" />
</UniformGrid>
</Grid>
这里的问题是 UniformGrid 使所有按钮保持相同的宽度。
如果我用 StackPanel
替换 UniformGrid
,则项目大小正确,但当它们超出最大大小时,StackPanel
可以增长到 (由于其固定大小的父级),它们被剪掉,而不是被迫进入较小的区域。
那么,有没有办法可以通过开箱即用的面板获得我想要的行为?
I'm looking for a WPF layout that works like a horizontal stack panel, such that items choose their own ideal width when there is enough space for them to fit, but additionally squashes items evenly when they don't fit in the available panel space.
I can get close to what I want with this:
<Grid Width="400" Height="80">
<UniformGrid Rows="1" HorizontalAlignment="Left">
<Button Content="Long Button" />
<Button Content="Very Long Button" />
<Button Content="Button" />
<Button Content="Button" />
<Button Content="Button" />
</UniformGrid>
</Grid>
The problem here being the UniformGrid
keeps all of the buttons the same width.
If I replace the UniformGrid
with a StackPanel
, then the items size correctly, but when they extend beyond the maximum size the StackPanel
can grow to (thanks to its fixed size parent), they are clipped out, instead of being forced into the smaller area.
So, is there a way I can get the behavior I want with the out-of-the box panels?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,如果您查看所有现有面板,您将看到没有一个符合要求,请实现您自己的。
No, if you look at all the existing panels you will see there is not one which fits the requirements, implement your own.
如果按钮的大小是静态的,您可以使用:
If the size of buttons is static, you can use this: