边框可以在不明确设置宽度的情况下填充 Stackpanel 的空间吗?

发布于 2024-12-25 11:24:46 字数 474 浏览 2 评论 0原文

下面是一些 xaml,它显示一个并排包含两个边框的 Stackpanel。我想让第二个边框填满Stackpanel的剩余区域,这样Stackpanel的红色背景就看不到了。但我现在看到了除了在 Border 上设置显式宽度之外的方法。可以在不设置显式宽度的情况下以某种方式完成吗?

<StackPanel Width="300" Background="Red" Orientation="Horizontal">
    <Border Width="100" Background="White">
        <TextBlock Text="Hello"/>
    </Border>
    <Border Background="Green">
        <TextBlock Text="World"/>
    </Border>
</StackPanel>

Here is some xaml that displays a Stackpanel containing two Borders side by side. I want to make the second Border fill up the remaining area of the Stackpanel so that the red background of the Stackpanel can't be seen. But I see now way to do it other than setting an explicit width on the Border . Can it be done somehow without setting an explicit width?

<StackPanel Width="300" Background="Red" Orientation="Horizontal">
    <Border Width="100" Background="White">
        <TextBlock Text="Hello"/>
    </Border>
    <Border Background="Green">
        <TextBlock Text="World"/>
    </Border>
</StackPanel>

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

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

发布评论

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

评论(1

听不够的曲调 2025-01-01 11:24:46

不使用 Stackpanel,不。 Stackpanel 不限制其子级的宽度。您应该为此布局使用 Dockpanel 或 Grid。

像这样的东西

<DockPanel Width="300" Background="Red">
    <Border Width="100" Background="White">
        <TextBlock Text="Hello"/>
    </Border>
    <Border Background="Green">
        <TextBlock Text="World"/>
    </Border>
</DockPanel>

Not with a Stackpanel, no. The Stackpanel does not limit the width of its children. You should use either a Dockpanel or a Grid for this Layout.

Something like

<DockPanel Width="300" Background="Red">
    <Border Width="100" Background="White">
        <TextBlock Text="Hello"/>
    </Border>
    <Border Background="Green">
        <TextBlock Text="World"/>
    </Border>
</DockPanel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文