Why isn't my StackPanel background color stretching to fill the full width?

I have a StackPanel with a background brush and it contains one button. It is contained in a grid that is wider than the button.

Disappointingly, the background is only as wide as the button. I can see this around the button box. So I get this default wide gray bar next to my button that has my background border around it.

<StackPanel Visibility="Visible" HorizontalAlignment="Right" Background="{StaticResource qtyBg}">
    <Button x:Name="bttnQtyEditKeys" Content="EDIT KEYPAD" Visibility="Visible" Click="bttnQtyEditKeys_Click"/>


I managed to fix it by wrapping a border around it and setting the border background brush.

This is a hack, so it annoys me. Is there an alternative way to make the StackPanel background not stupid?


source to share

1 answer

The problem isn't your background, it's your StackPanel. Your background is fine, it's your StackPanel not filling the available space.

You need to set HorizontalAlignment and VerticalAlignment to StackPanel.

This works great for me:

    <StackPanel Visibility="Visible" HorizontalAlignment="Stretch" Background="blue">
        <Button x:Name="bttnQtyEditKeys" HorizontalAlignment="Right" Content="EDIT KEYPAD" Visibility="Visible" Click="bttnQtyEditKeys_Click"/>




All Articles