I’m a beginner in WPF, I put below code in a button click handler, I expect there would be 10 vertical lines after clicking the button, but there’s only one, any ideas? Thanks! And ‘spDrawPanel’ is a stackpanel.
double aw = this.spDrawPanel.ActualWidth;
double ah = this.spDrawPanel.ActualHeight;
for (int i = 1; i <= 10; i++)
{
Line ln = new Line();
ln.Stroke = Brushes.Black;
ln.X1 = aw / 10 * i;
ln.Y1 = ah;
ln.X2 = aw / 10 * i;
ln.Y2 = ah - 15;
ln.StrokeThickness = 1;
this.spDrawPanel.Children.Add(ln);
}
The problem is that you are using a
StackPanelwhich by default stacks each line Verticaly from each other and you are starting at the bottom of theStackPaneland hence you are running out of real estate. Try setting Y1 to 15 and Y2 to 0 and you will get your 10 lines.