Silverlight: stretching to remaining space in StackPanel

前端 未结 4 862
无人及你
无人及你 2021-02-01 14:07

I have a vertical StackPanel with two elements: a Button and a ListBox. How can I have the ListBox stretch to the remaining page height?



        
相关标签:
4条回答
  • 2021-02-01 14:37

    Use UniformGrid Columns="2" instead of StackPanel.

    0 讨论(0)
  • 2021-02-01 14:44

    You can use a DockPanel. Set the first item to dock top and the second to dock fill, or use the LastChildFill property:

    <toolkit:DockPanel LastChildFill="True"
     xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit">    
        <Button DockPanel.Dock="Top" Height="30" Width="100" 
         Content="Get Content" x:Name="GetContent"/>
        <ListBox Background="Azure" />
    </toolkit:DockPanel>
    
    0 讨论(0)
  • 2021-02-01 14:50

    Well, you already found the solution ;) StackPanels won't fill any remaining space by default because their size is always equal to the combined required size of their child elements. Grid is a great way to go because it will dynamically resize when the browser size changes. Mark's answer of using a DockPanel works fine too. The only other method would be to manually size the elements when the parent control's size changes. In general through, stick with Grids for the outer layout and they fill them up with StackPanels and other controls and you should be set.

    0 讨论(0)
  • 2021-02-01 14:59

    I agree with James' observation that "StackPanels won't fill any remaining space by default because their size is always equal to the combined required size of their child elements." That is exactly what happens, but this isn't the expected result.

    I can see why this instruction would compress the contents into the smallest necessary space.

    <StackPanel Height="Auto" Width="Auto">
    

    However... StackPanel accepts a HorizontalAlignment argument - which if selected should cause the stack panel to fill the contents of its parent container. This is exactly what happens when you set Orientation="Vertical". Observe that in this case the StackPanel will determine how much horizontal space is available and allocated it to the child controls.

    <StackPanel Height="30" Orientation="Vertical" HorizontalAlignment="Stretch" > 
       <TextBox /> <!--TextBox fills entire space-->
    </StackPanel>
    

    Its only when Orientation="Horizontal" that the Horizontal child sizing breaks down.

    <StackPanel Height="30" Orientation="Horizontal" HorizontalAlignment="Stretch" > 
       <TextBox /> <!--TextBox fills smallest space available-->
    </StackPanel>
    
    0 讨论(0)
提交回复
热议问题