简体   繁体   English

WPF:如何在StackPanel中进行控件拉伸?

[英]WPF: How to make controls stretch in a StackPanel?

A Slider and some other controls won't stretch to fill available space when placed in a StackPanel; 放置在StackPanel中时,Slider和其他一些控件不会拉伸以填充可用空间; instead the width is always MinWidth (or about 10 pixels if MinWidth is not set). 相反,宽度始终为MinWidth(如果未设置MinWidth,则为大约10个像素)。 How do I make it stretch (the equivalent of Anchor.Left|Anchor.Right in WinForms)? 如何使其伸展(相当于WinForms中的Anchor.Left | Anchor.Right)? 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>

You want DockPanel instead: 你想要DockPanel:

<DockPanel DockPanel.Dock="Bottom" LastChildFill="True">
    <TextBlock DockPanel.Dock="Right" ... />
    <Slider ... />
</DockPanel>

DockPanel gets you the "Anchor" functionality you want. DockPanel为您提供所需的“锚点”功能。 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. 我从你的代码块中注意到你的StackPanel已经停靠在dockpanel中,所以我在上面的代码示例中包含了相同的dock属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM