2009-12-27 80 views
8

我试图根据其外部空间(它是在一个StackPanel),使一个TabControl来自动调整:TabControl.VerticalAlignment =弹力不会做任何事情

<Window x:Class="Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="100"> 
    <Grid> 
     <StackPanel> 
      <TabControl 
       BorderBrush="Red" 
       BorderThickness="2" 
       VerticalAlignment="Stretch" 
       VerticalContentAlignment="Stretch"> 

       <TabItem Header="Tab1"/> 
       <TabItem Header="Tab2"/> 
      </TabControl> 
     </StackPanel> 
    </Grid> 
</Window> 

上面的代码片段生成以下窗口,而我想要的红色边框到达窗口的底部:

alt text

回答

19

的问题是你StackPanel。 StackPanels不会伸展他们的孩子。

改为使用DockPanel:最后一个孩子将被拉伸以填充剩余空间(请参阅LastChildFill,默认为true)。

<Window x:Class="Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="100"> 
    <Grid> 
     <DockPanel> 
      <TabControl BorderBrush="Red" BorderThickness="2"> 
       <TabItem Header="Tab1"/> 
       <TabItem Header="Tab2"/> 
      </TabControl> 
     </DockPanel> 
    </Grid> 
</Window> 

明确设置VerticalAlignment是没有必要的,因为its default value已经Stretch

相关链接:Panels Overview on MSDN

+0

我用你的代码,什么都没有; TabControl的垂直尺寸未按预期停靠;我认为问题出在TabControl而不是StackPanel上。 – Shimmy 2009-12-27 23:54:11

+0

非常奇怪......它在这里完美地运行得很好:我将上面的代码复制并粘贴到XAML窗口中 - >红色边框横跨整个窗口。你能再试一次吗? – Heinzi 2009-12-27 23:58:16

+3

你的解决方案确实有效。不幸的是,根据我的窗户的情况,有不同的父母,它没有工作。我解决特定问题的方法是将其嵌入到Grid中,将行的高度设置为*。 感谢您的帮助。 – Shimmy 2009-12-28 00:48:15

4

您可以绑定的高度,以父窗口的实际高度。

<TabControl 
    BorderBrush="Red" 
    BorderThickness="2" 
    Height="{Binding Path=ActualHeight, 
     RelativeSource={RelativeSource Mode=FindAncestor, 
      AncestorType={x:Type Window}}}"> 
    <TabItem Header="Tab1"/> 
    <TabItem Header="Tab2"/> 
</TabControl>