Set a border around a StackPanel.

What about this one : <DockPanel Margin=”8″> <Border CornerRadius=”6″ BorderBrush=”Gray” Background=”LightGray” BorderThickness=”2″ DockPanel.Dock=”Top”> <StackPanel Orientation=”Horizontal”> <TextBlock FontSize=”14″ Padding=”0 0 8 0″ HorizontalAlignment=”Center” VerticalAlignment=”Center”>Search:</TextBlock> <TextBox x:Name=”txtSearchTerm” HorizontalAlignment=”Center” VerticalAlignment=”Center” /> <Image Source=”lock.png” Width=”32″ Height=”32″ HorizontalAlignment=”Center” VerticalAlignment=”Center” /> </StackPanel> </Border> <StackPanel Orientation=”Horizontal” DockPanel.Dock=”Bottom” Height=”25″ /> </DockPanel>

What is the difference between a StackPanel and DockPanel in WPF?

Stack Panel: The StackPanel, as the name implies, arranges content either horizontally or vertically. Vertical is the default, but this can be changed using the Orientation property. Content is automatically stretched based on the orientation (see screenshot below), and this can be controlled by changing the HorizontalAlignment or VerticalAlignment properties. Dock Panel: The DockPanel is … Read more

Padding on StackPanel?

You could put a Border around the StackPanel and set a padding on that. I end up doing this a lot, since there are many UIElements that do not have a padding property. <Border Padding=”10″> <StackPanel> <!–…–> </StackPanel> </Border> (Note: all FrameworkElements have a Margin property, which will space the element, but not include the … Read more

Aligning controls on both left and right side in a stack panel in WPF

Just do not use a StackPanel, StackPanels stack. They do, for obvious reasons, not allow alignment in the direction in which they stack. Use a Grid, with column definitions like so: <Grid.ColumnDefinitions> <ColumnDefinition Width=”Auto” /> <ColumnDefinition Width=”*” /> <ColumnDefinition Width=”Auto” /> </Grid.ColumnDefinitions>

Align items in a stack panel?

You can achieve this with a DockPanel: <DockPanel Width=”300″> <TextBlock>Left</TextBlock> <Button HorizontalAlignment=”Right”>Right</Button> </DockPanel> The difference is that a StackPanel will arrange child elements into single line (either vertical or horizontally) whereas a DockPanel defines an area where you can arrange child elements either horizontally or vertically, relative to each other (the Dock property changes the … Read more

How do I space out the child elements of a StackPanel?

Use Margin or Padding, applied to the scope within the container: <StackPanel> <StackPanel.Resources> <Style TargetType=”{x:Type TextBox}”> <Setter Property=”Margin” Value=”0,10,0,0″/> </Style> </StackPanel.Resources> <TextBox Text=”Apple”/> <TextBox Text=”Banana”/> <TextBox Text=”Cherry”/> </StackPanel> EDIT: In case you would want to re-use the margin between two containers, you can convert the margin value to a resource in an outer scope, f.e. … Read more