2016-08-13 57 views
0

我与此类似下面的示例XAML定义布局的WPF应用程序的工作:如何让ItemsControl适合其内容,而不是垂直扩展而不考虑它的兄弟元素?

<Window> 
    <Grid x:Name="LayoutRoot"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="5*" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 

     <Border Grid.Column="0" Background="AliceBlue" /> 

     <!-- Main Panel --> 
     <Grid Grid.Column="1" 
       Background="LightPink"> 

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

      <!-- Row 0: some User Control --> 
      <Grid Grid.Row="0" MinHeight="100" /> 

      <!-- Row 1: some label --> 
      <Label Grid.Row="1" Content="Example text" /> 

      <!-- Row 2: another User Control --> 
      <StackPanel Grid.Row="2"> 

       <ScrollViewer VerticalScrollBarVisibility="Auto"> 
        <ItemsControl> 
         <ItemsControl.Items> 
          <ContentPresenter Content="Item 1" /> 
          <ContentPresenter Content="Item 2" /> 
          <ContentPresenter Content="Item 3" /> 
          <ContentPresenter Content="Item 4" /> 
          <ContentPresenter Content="Item 5" /> 
          <ContentPresenter Content="Item 6" /> 
         </ItemsControl.Items> 
        </ItemsControl> 
       </ScrollViewer> 

       <Button Content="Open" /> 
      </StackPanel> 

     </Grid> 
     <Border Grid.Column="2" Background="AliceBlue" /> 

    </Grid> 
</Window> 

目测它的意图是这样的: Application layout

简而言之,布局根是一个包含3列的网格;左边和右边的列只是间隔符,中间的列包含2个用户控件(在我上面的例子中用2个网格表示)。

我想实现如下:

  1. 如果有足够的垂直空间,我想ItemsControl的显示没有滚动条和按钮,出现正下方的物品(不其包含面板的底部)。

  2. 如果没有足够的垂直空间,我希望ItemsControl显示一个滚动条而不是垂直有效地将按钮踢出视图。

所需布局的可视例子是如下面的截图: Desired layout

一切我试图似乎之一:

  1. 保持停靠至底部时打开按钮有足够的空间,或者
  2. 当由于ItemsControl垂直扩展而导致空间不足时,将Open按钮踢出视图。

有什么方法可以实现所需的布局(无论是在XAML还是代码中)?

回答

1

通过顶部对齐网格更换的StackPanel:

<!-- Row 2: another User Control --> 
<Grid VerticalAlignment="Top" Grid.Row="2"> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 

    <ScrollViewer VerticalScrollBarVisibility="Auto"> 
     <ItemsControl> 
      ... 
     </ItemsControl> 
    </ScrollViewer> 

    <Button Grid.Row="1" HorizontalAlignment="Left" Content="Open" /> 
</Grid> 
如果没有足够的空间的ItemsControl将扩大垂直试图满足其项目有效地踢打开按钮显示出来的
+0

不会因为工作。 –

+0

@ AhmedA.Hamid事实并非如此。抱怨之前,你应该简单地尝试一下。 – Clemens

+0

试过了。它不会将Open按钮踢出显示屏,但是如果有足够的空间,它将按钮保持在底部,这不是我想要达到的目的(查看问题中的视觉插图)。 –

相关问题