WPF:如何使控件在 StackPanel 中拉伸? [英] WPF: How to make controls stretch in a StackPanel?
问题描述
Slider 和其他一些控件在放置在 StackPanel 中时不会拉伸以填充可用空间;相反,宽度始终为 MinWidth(如果未设置 MinWidth,则为大约 10 个像素).我如何让它拉伸(相当于 WinForms 中的 Anchor.Left|Anchor.Right)?示例:
A Slider and some other controls won't stretch to fill available space when placed in a StackPanel; instead the width is always MinWidth (or about 10 pixels if MinWidth is not set). How do I make it stretch (the equivalent of Anchor.Left|Anchor.Right in WinForms)? Example:
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
<Slider Value="14" SmallChange="0.5" Maximum="100" Minimum="4"
x:Name="FontSize" MinWidth="30" LargeChange="2"
TickFrequency="10" TickPlacement="TopLeft"
HorizontalAlignment="Stretch"/>
<TextBlock Margin="8" Text="{Binding ElementName=FontSize, Path=Value}"
VerticalAlignment="Center"/>
</StackPanel>
推荐答案
你想要 DockPanel 代替:
You want DockPanel instead:
<DockPanel DockPanel.Dock="Bottom" LastChildFill="True">
<TextBlock DockPanel.Dock="Right" ... />
<Slider ... />
</DockPanel>
DockPanel 为您提供所需的锚点"功能.StackPanel 只是将东西彼此相邻堆叠.
DockPanel gets you the "Anchor" functionality you want. StackPanel simply stacks things next to each other.
我从您的代码块中注意到您的 StackPanel 已经停靠在停靠面板内,因此我在上面的代码示例中包含了相同的停靠属性.
I notice from your code block that your StackPanel is already docked inside a dockpanel, so I've included the same docking attribute in my code sample above.
这篇关于WPF:如何使控件在 StackPanel 中拉伸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!