代码之家  ›  专栏  ›  技术社区  ›  Aran Mulholland JohnnyAce

列表框或ScrollViewer中的其他项控件

  •  2
  • Aran Mulholland JohnnyAce  · 技术社区  · 14 年前

    我们可以用下面的代码来模拟我们的问题。

    <Window
       x:Class="WpfApplication1.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title="MainWindow"
       Height="150"
       Width="525">
    
       <Grid
          Margin="10">
    
          <ScrollViewer>
             <Grid>
                <Grid.RowDefinitions>
                   <RowDefinition
                      Height="*" />
                   <RowDefinition
                      Height="Auto" />
                </Grid.RowDefinitions>
    
                <Border
                   x:Name="border"
                   MinHeight="40" />
    
                <ListBox
                   Height="{Binding ElementName=border, Path=ActualHeight}">
                   <ListBoxItem>Item 1</ListBoxItem>
                   <ListBoxItem>Item 1</ListBoxItem>
                   <ListBoxItem>Item 1</ListBoxItem>
                   <ListBoxItem>Item 1</ListBoxItem>
                   <ListBoxItem>Item 1</ListBoxItem>
                   <ListBoxItem>Item 1</ListBoxItem>
                   <ListBoxItem>Item 1</ListBoxItem>
                   <ListBoxItem>Item 1</ListBoxItem>
                   <ListBoxItem>Item 1</ListBoxItem>
                   <ListBoxItem>Item 1</ListBoxItem>
                   <ListBoxItem>Item 1</ListBoxItem>
                   <ListBoxItem>Item 1</ListBoxItem>
                </ListBox>
    
                <ToolBar
                   Grid.Row="1">
                   <Button
                      Content="Add" />
                </ToolBar>
             </Grid>
    
          </ScrollViewer>
       </Grid>
    </Window>
    

    如何让列表框调整大小,就像没有列表框时边框调整大小一样?

    2 回复  |  直到 13 年前
        1
  •  1
  •   dnr3    14 年前

    我不知道你想在这里做什么,但是如果你想让列表框在窗口高度降低时降低它的高度,那么你必须在边框上添加边距

            <Border
               Margin="5"
               x:Name="border"
               MinHeight="40" />
    

    注:

    这将使列表框的高度降低,但随后它可能会激活列表框的内部滚动,因此您将看到两个滚动条在那里。

        2
  •  1
  •   Martin Harris    14 年前

    不是100%确定你在找什么,但我想

    <ListBox HorizontalAlignment="Stretch"
             VerticalAlignment="Stretch">
    

    可能是你需要的。它将缩小列表框,直到它达到最小大小(显示所有项目),然后激活滚动条。