I am creating a new user control. My control extends a StackPanel, and for now consists of another StackPanel in which I'm adding some Rectangles. I would like the inner StackPanel to be horizontally centered within the control, and the Rectangles to be vertically aligned with the bottom of the inner StackPanel. My code is below, but it results in the inner StackPanel aligned to the left of the control, and the Rectangles are aligned at the top of the inner StackPanel.
public partial class ComponentStacker : StackPanel
{
private StackPanel tokenHolder;
public ComponentStacker(int numTokens)
{
this.Orientation = Orientation.Horizontal;
tokenHolder = new StackPanel();
this.Children.Add(tokenHolder);
tokenHolder.Background = new SolidColorBrush(Colors.DarkKhaki);
tokenHolder.HorizontalAlignment = HorizontalAlignment.Center;
for (int i = 0; i < numTokens; i++)
{
Rectangle rect = new Rectangle();
rect.Width = 15;
rect.Height = 10;
rect.Margin = new Thickness(5, 5, 5, 0);
rect.Fill = new SolidColorBrush(Colors.Red);
rect.VerticalAlignment = VerticalAlignment.Bottom;
this.tokenHolder.Children.Add(rect);
}
this.Background = new SolidColorBrush(Colors.Goldenrod);
this.Margin = new Thickness(10);
}
}