【问题标题】:wpf stack panelwpf堆栈面板
【发布时间】:2011-07-11 22:29:18
【问题描述】:

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

<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>

【问题讨论】:

    标签: wpf xaml stackpanel


    【解决方案1】:

    尝试使用DockPanel,将ToolBar 停靠在顶部或底部,并将DockPanel 上的LastChildFill 属性设置为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>
    

    【讨论】:

      【解决方案2】:

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

      我会改用网格。

      【讨论】:

        【解决方案3】:

        使用 DockPanel 代替 StackPanel 并将 LastChildFill 属性设置为 true。这应该可以解决问题。

        【讨论】:

          【解决方案4】:

          尝试使用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>
          

          【讨论】:

          • 哦,你知道我现在有多傻吗?我期待 StackPanel 像 DockPanel 一样工作。谢谢大家。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-24
          • 1970-01-01
          • 2012-12-07
          • 1970-01-01
          相关资源
          最近更新 更多