Search code examples
c#wpfstackpanel

StackPanel space between each item at runtime


Ok I have a stack panel and I add child at runtime like so

UserC.ShowData sD = new UserC.ShowData();
ShowDataStackPanel.Children.Add(sD);

I know I can define the margin if I add the item before the runtime but I,ve looked and it's not very clear how to add a user control with a margin at runtime.

Edit

The stackpanel is defined like so

<StackPanel x:Name="ShowDataStackPanel" HorizontalAlignment="Left" Margin="727,33,0,0" VerticalAlignment="Top" Height="617" Width="247"/>

Solution

  • Use Margin propriety for the sD control :

    sD.Margin = new Thickness(12,12,0,0);