2012-03-06 104 views
3

我无法让我的WPF布局工作。我希望ListBox通过锚定到窗口的底部来垂直拉伸。它目前只是调整到StackPanel(添加和删除按钮)中控件的高度,并调整大小以适应添加的项目。在WinForms中,我只是将ListView.Anchor设置为Top|Left|Bottom|Right,但我不应该生活在过去。我已经尝试了一些东西,比如将它放在DockPanel中,将所有内容都包装在Canvas等中,但似乎没有任何影响。获取WPF控件垂直拉伸

ViewsTestListView

这是我的XAML:

<Window x:Class="FileDropAdmin.ViewsTestListView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
     xmlns:shared="http://schemas.markpad.net/winfx/xaml/shared" 
     Title="ViewsTestListView" Height="300" Width="416"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 

     <TextBlock TextWrapping="Wrap" VerticalAlignment="Top" Text="Things:" /> 
     <Grid Grid.Row="1"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="4*"/> 
       <ColumnDefinition Width="1*"/> 
      </Grid.ColumnDefinitions> 

      <ListBox x:Name="Things" DisplayMemberPath="ThingName" SelectedItem="CurrentThing" Grid.Column="0"/> 
      <StackPanel Margin="5 0 0 0" VerticalAlignment="Top" Grid.Column="1"> 
       <Button x:Name="AddThing" Content="Add" Margin="0 0 0 0" VerticalAlignment="Top"/> 
       <Button x:Name="RemoveThing" Content="Remove" Margin="0 5 0 0" VerticalAlignment="Top"/> 
      </StackPanel> 
     </Grid> 
    </Grid> 
</Window> 

回答

6

设置你的第二行以高度=“*”,并应采取窗口的所有空间,如果您需要什么

+0

完美!谢谢:-D – 2012-03-06 06:19:17

0

添加另一行,占用了所有剩余的空间...

<Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 

...然后调整行值文本块和列表框...

<TextBlock Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Text="Things:" /> 
    <Grid Grid.Row="2"> 
+0

这似乎有效地停靠在底部这是不是我想要的,但谢谢 – 2012-03-06 06:18:25

0

你需要下面的选项...

你第二行确定指标必须Height="*"

<Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 

和您的列表框需要ColumnSpan="2"

 <ListBox x:Name="Things" 
       DisplayMemberPath="ThingName" 
       SelectedItem="CurrentThing" 
       Grid.ColumnSpan="2" 
       Grid.Column="0"/> 

另外你说你使用DockPanel中......与dockpa它更容易。所有你必须设置它LastChildFill =“True”,并添加您的ListBox作为docksetnel中的最后一个孩子。

<DockPanel LastChildFill="True"> 
      <Grid DockPanel.Dock="Top" HorizontalAlignment="Stretch"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="4*"/> 
        <ColumnDefinition Width="1*"/> 
       </Grid.ColumnDefinitions> 
       <StackPanel Margin="5 0 0 0" Grid.Column="0" 
          HorizontalAlignment="Stretch"> 
        <TextBlock Text="Things:"/> 
        <TextBox HorizontalAlignment="Stretch" 
           VerticalAlignment="Stretch"/> 
       </StackPanel> 
       <StackPanel Margin="5 0 0 0" Grid.Column="1"> 
        <Button x:Name="AddThing" Content="Add" Margin="0 0 0 0"/> 
        <Button x:Name="RemoveThing" Content="Remove" 
          Margin="0 5 0 0"/> 
       </StackPanel> 
      </Grid> 
      <ListBox x:Name="Things" DisplayMemberPath="ThingName" 
        SelectedItem="CurrentThing" /> 
    </DockPanel>