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>
Put ScrollViewer inside StackPanel . You could use a ListBox instead. It will scroll automatically.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With