我正在尝试使 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>
上面的代码片段产生以下窗口,而我希望红色边框到达窗口的底部:
最佳答案
问题是你的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/