面板布局可均匀挤压项目以适应

发布于 2025-01-08 19:27:22 字数 761 浏览 5 评论 0原文

我正在寻找一个像水平堆栈面板一样工作的 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 技术交流群。

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

发布评论

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

评论(2

少女七分熟 2025-01-15 19:27:22

那么,有没有办法可以通过开箱即用的面板获得我想要的行为?

不,如果您查看所有现有面板,您将看到没有一个符合要求,请实现您自己的。

So, is there a way I can get the behavior I want with the out-of-the box panels?

No, if you look at all the existing panels you will see there is not one which fits the requirements, implement your own.

你是暖光i 2025-01-15 19:27:22

如果按钮的大小是静态的,您可以使用:

<Grid Width="400" Height="80">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*"/>
        <ColumnDefinition Width="3*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Button Grid.Column="0" Content="Long Button" />
    <Button Grid.Column="1" Content="Very Long Button" />
    <Button Grid.Column="2" Content="Button" />
    <Button Grid.Column="3" Content="Button" />
    <Button Grid.Column="4" Content="Button" />
</Grid>

If the size of buttons is static, you can use this:

<Grid Width="400" Height="80">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*"/>
        <ColumnDefinition Width="3*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Button Grid.Column="0" Content="Long Button" />
    <Button Grid.Column="1" Content="Very Long Button" />
    <Button Grid.Column="2" Content="Button" />
    <Button Grid.Column="3" Content="Button" />
    <Button Grid.Column="4" Content="Button" />
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文