Is there an easy way to set default space between items inside StackPanel so I'll don't have to set Margin property on each item?
-
4I would say Margin is the easiest way to go here... are they typically items of the same type? You can use implicit styles to set the Margin for each item... – kiwipom Mar 14 '11 at 23:58
-
Maybe I can use styles to set Margin for some base type? 'Control' for example – Poma Mar 15 '11 at 00:04
-
1Also see the accepted answer here: http://stackoverflow.com/questions/932510/how-do-i-space-out-the-child-elements-of-a-stackpanel – Mike Jun 18 '14 at 21:35
5 Answers
I use a transparent separator, which works well:
<Separator Opacity="0" Height="20"/>
You can of course use margins but then if you want to change the margins you have to update all of the elements.
The separator can even be styled in a static resource.
An attached property could do it too but I think it's overkill.

- 20,529
- 24
- 107
- 134

- 2,762
- 3
- 24
- 27
-
-
How do I use this? Do I have to manual add a `Seperator` element after each child element? I don't see how this is better than setting the `Margin` property directly each child item. Am I missing something? – Dan Stevens Nov 13 '17 at 15:39
if all the controls are the same then do as IanR suggested and implement a Style that catches that control. if it's not then you can't create a default style to a base class because it just won't work.
the best way for situations like these is to use a very neat trick - attached properties (aka Behaviors in WPF4)
you can create a class that has an attached property, like so:
public class MarginSetter
{
public static Thickness GetMargin(DependencyObject obj)
{
return (Thickness)obj.GetValue(MarginProperty);
}
public static void SetMargin(DependencyObject obj, Thickness value)
{
obj.SetValue(MarginProperty, value);
}
// Using a DependencyProperty as the backing store for Margin. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MarginProperty =
DependencyProperty.RegisterAttached("Margin", typeof(Thickness), typeof(MarginSetter), new UIPropertyMetadata(new Thickness(), CreateThicknesForChildren));
public static void CreateThicknesForChildren(object sender, DependencyPropertyChangedEventArgs e)
{
var panel = sender as Panel;
if (panel == null) return;
foreach (var child in panel.Children)
{
var fe = child as FrameworkElement;
if (fe == null) continue;
fe.Margin = MarginSetter.GetMargin(panel);
}
}
}
now, to use it, all you need to do is to attach this attached property to any panel you want, like so:
<StackPanel local:MarginSetter.Margin="10">
<Button Content="hello " />
<Button Content="hello " />
<Button Content="hello " />
<Button Content="hello " />
</StackPanel>
Completely reusable of course.

- 7,483
- 5
- 35
- 66
-
2The only problem is that it works statically: if you were adding/removing children once the panel has been instantiated, that won't work. – Mario Vernari Mar 15 '11 at 05:18
-
2Your attached behavior could maybe subscribe to `LayoutUpdated`, and set the correct margin for new items as needed. – Jens Mar 15 '11 at 07:42
-
-
@Mario - you're right, but i basically gave the general direction. Jens's idea would be a good way to continue - it's easy to solve the general case. – Elad Katz Mar 15 '11 at 10:41
-
1At runtime CreateThicknesForChildren event is invoked befora any children are added. How to fix this code so it'll work not only on designer? – Poma Mar 29 '11 at 15:41
-
1register to one of the events of the panel that would happen after the children has already been added. i'm guessing Loaded might do the trick. – Elad Katz Mar 29 '11 at 19:13
-
1How conveniently cumbersome is WPF in comparison to HTML and CSS (LESS, SASS) when it comes to markup. – Mike de Klerk Jan 22 '16 at 17:25
-
This is not going to work (any longer) and - for the children of the panel to be loaded - I have to subscribe the following event `private void StackPanel_Loaded(object sender, RoutedEventArgs e) { MarginSetter.CreateThicknesForChildren(sender, new DependencyPropertyChangedEventArgs()); }` otherwise at the attached property initialization the `panel.Children` has zero count and the foreach of `CreateThicknesForChildren` is skipped – Feb 05 '17 at 12:44
I find that creating a grid inside the stack panel, then adding the desired number of columns (or rows) as follows:
<StackPanel Grid.Row="1" Grid.Column="0" Height="34" Width="698" Margin="10,5,10,10" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" >
<Grid Width="698" Margin="0,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="StartButton" Content="Start" Grid.Row="0" Grid.Column="0" Style="{StaticResource 3DButton}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="70" Click="StartButton_Click" />
<Button x:Name="HelpButton" Content="Help" Grid.Row="0" Grid.Column="1" Style="{StaticResource 3DButton}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="70" Click="HelpButton_Click" />
<Button x:Name="ExitButton" Content="Exit" Grid.Row="0" Grid.Column="2" Style="{StaticResource 3DButton}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="70" Click="ExitButton_Click" Foreground="Red" />
</Grid>
</StackPanel>

- 37
- 9
-
You could just omit the stackpanel and get the same effect. The Grid will stretch to its parent by default, by the way. Giving it a fixed width is unnecessary, and so a bad idea, as the parent may be resized later. WPF layouts use a lot of auto-sizing and stretching to parents. – 15ee8f99-57ff-4f92-890c-b56153 Jun 18 '17 at 01:09
The accepted answer doesn't work anymore. But I used that answer and the blog the author of that answer (Elad Katz) to make a working code (tested in .Net Core) that I reproduce here:
public static class EstablecedorMargen {
public static Thickness GetMargen(DependencyObject objeto) => objeto != null ? (Thickness)objeto.GetValue(PropiedadMargen) : new Thickness();
public static void SetMargen(DependencyObject objeto, Thickness value) => objeto?.SetValue(PropiedadMargen, value);
public static readonly DependencyProperty PropiedadMargen
= DependencyProperty.RegisterAttached("Margen", typeof(Thickness), typeof(EstablecedorMargen), new UIPropertyMetadata(new Thickness(), Cambió));
public static void Cambió(object sender, DependencyPropertyChangedEventArgs e) {
if (!(sender is Panel panel)) return;
panel.Loaded += new RoutedEventHandler(EstablecerMargenControlesHijos);
}
public static void EstablecerMargenControlesHijos(object sender, RoutedEventArgs e) {
if (!(sender is Panel panel)) return;
foreach (var hijo in panel.Children) {
if (!(hijo is FrameworkElement feHijo)) continue;
feHijo.Margin = GetMargen(panel);
}
}
}
Then you use:
<StackPanel local:EstablecedorMargen.Margen="10" >
<Button Content="1" />
<Button Content="2" />
<Button Content="3" />
</StackPanel>

- 406
- 5
- 7
-
Leaving some English code would be much appreciated, that way people can follow the code much more easily. This is an international community, not a Spanish one. – Mark Jun 06 '22 at 00:58