9

我正在尝试使 TabControl 根据其外部空间自动调整大小(它在 StackPanel 中):

<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>

上面的代码片段产生了以下窗口,而我希望红色边框到达窗口的底部:

替代文字

4

2 回答 2

22

问题是你的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不需要显式设置,因为它的默认值已经是Stretch.

相关链接:MSDN 上的面板概述

于 2009-12-27T21:44:37.033 回答
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>
于 2012-01-20T01:16:01.073 回答