Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: How to make controls stretch in a StackPanel?

Tags:

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> 
like image 442
Qwertie Avatar asked May 06 '09 23:05

Qwertie


People also ask

How do you make a StackPanel scroll?

Put ScrollViewer inside StackPanel . You could use a ListBox instead. It will scroll automatically.

What is the difference between StackPanel and DockPanel?

For example, the order of child elements can affect their size in a DockPanel but not in a StackPanel. This is because StackPanel measures in the direction of stacking at PositiveInfinity, whereas DockPanel measures only the available size. The following example demonstrates this key difference.

What does StackPanel do in WPF?

A StackPanel allows you to stack elements in a specified direction. By using properties that are defined on StackPanel, content can flow both vertically, which is the default setting, or horizontally.


1 Answers

You want DockPanel instead:

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

DockPanel gets you the "Anchor" functionality you want. StackPanel simply stacks things next to each other.

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.

like image 132
Matt Hamilton Avatar answered Sep 24 '22 02:09

Matt Hamilton