WPF堆栈面板

发布于 2024-10-21 04:40:39 字数 820 浏览 2 评论 0原文

简单的问题,我有一个带有工具栏和列表框的堆栈面板,我希望列表框填充剩余空间,但它不会。这就是我现在所拥有的。

<Window x:Class="TestClientMainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Testing client" Height="350" Width="525"
    DataContext="{StaticResource ResourceKey=TheViewModel}" Background="#FFD4BFBF">
<Grid>
    <StackPanel HorizontalAlignment="Stretch" Name="stackPanel1" VerticalAlignment="Stretch">
        <ToolBar Height="26" Name="toolBar1" />
        <ListBox Name="listBox1" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" MinHeight="{Binding ElementName=stackPanel1, Path=Height}" Height="99" />
    </StackPanel>
</Grid>

Easy question, I have a stack panel with a toolbar and listbox, I want the listbox to fill the remaining space, but it won't. Here's what I have at the moment.

<Window x:Class="TestClientMainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Testing client" Height="350" Width="525"
    DataContext="{StaticResource ResourceKey=TheViewModel}" Background="#FFD4BFBF">
<Grid>
    <StackPanel HorizontalAlignment="Stretch" Name="stackPanel1" VerticalAlignment="Stretch">
        <ToolBar Height="26" Name="toolBar1" />
        <ListBox Name="listBox1" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" MinHeight="{Binding ElementName=stackPanel1, Path=Height}" Height="99" />
    </StackPanel>
</Grid>

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

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

发布评论

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

评论(4

若言繁花未落 2024-10-28 04:40:39

尝试使用 DockPanel 而不是 StackPanel。这样,您可以将 LastChildFill 设置为 true,并且您的 ListBox 作为最后一个包含的元素,将拉伸以填充其剩余空间:

<DockPanel LastChildFill="True" HorizontalAlignment="Stretch" Name="dockPanel1" VerticalAlignment="Stretch">
    <ToolBar Height="26" Name="toolBar1" />
    <ListBox Name="listBox1" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" MinHeight="{Binding ElementName=stackPanel1, Path=Height}" Height="99" />
</DockPanel>

Try using a DockPanel instead of a StackPanel. That way you can set LastChildFill to true and your ListBox, being the last contained element, will stretch to fill up its remaining space:

<DockPanel LastChildFill="True" HorizontalAlignment="Stretch" Name="dockPanel1" VerticalAlignment="Stretch">
    <ToolBar Height="26" Name="toolBar1" />
    <ListBox Name="listBox1" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" MinHeight="{Binding ElementName=stackPanel1, Path=Height}" Height="99" />
</DockPanel>
梦明 2024-10-28 04:40:39

尝试使用 DockPanel,将 ToolBar 停靠在顶部或底部,并将 LastChildFill 属性设置在 DockPanel 上为true

<DockPanel Name="dockPanel1" LastChildFill="True">
    <ToolBar Height="26" Name="toolBar1" DockPanel.Dock="Top"/>
    <ListBox Name="listBox1" MinHeight="{Binding ElementName=stackPanel1, Path=Height}" Height="99" />
</DockPanel>

Try using a DockPanel, with the ToolBar docked to the top or bottom and the LastChildFill property on the DockPanel set to true.

<DockPanel Name="dockPanel1" LastChildFill="True">
    <ToolBar Height="26" Name="toolBar1" DockPanel.Dock="Top"/>
    <ListBox Name="listBox1" MinHeight="{Binding ElementName=stackPanel1, Path=Height}" Height="99" />
</DockPanel>
三生池水覆流年 2024-10-28 04:40:39

使用 DockPanel 而不是 StackPanel 并将 LastChildFill 属性设置为 true。那应该可以解决问题。

Use DockPanel instead of StackPanel and set LastChildFill attribute to true. That should do the trick.

夏末染殇 2024-10-28 04:40:39

StackPanel 不会轻易被说服做你想做的事。

我会使用网格代替。

A StackPanel will not be easily convinced to do what you want.

I would use a Grid instead.

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