wpf - TabControl.VerticalAlignment = 拉伸(stretch)不做任何事情

标签 wpf layout vertical-alignment tabcontrol

我正在尝试使 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>

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

alt text

最佳答案

问题是你的StackPanel . StackPanels 不会拉伸(stretch)他们的 child 。

相反,请使用 DockPanel :最后一个 child 将被拉伸(stretch)以填充剩余空间(参见 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

关于wpf - TabControl.VerticalAlignment = 拉伸(stretch)不做任何事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1967013/

相关文章:

c# - 设置UIaccess更改ShowDialog的行为

angularjs - 如何在布局内水平滚动= Angular Material 中的行

css - 伪类垂直对齐

即使窗口透明,具有半透明模糊背景的 WPF 圆角窗口角仍然模糊

c# - 运行 WPF 应用程序时出错

WPF UserControl 或 ControlTemplate...(不确定)

html - 响应式两列页脚

android - RecyclerView 裁剪底部数据

html - 两个垂直对齐的 div 旁边的 div

html - 使用边框半径在 div 中居中文本