Weston Goodwin
Weston Goodwin

Reputation: 343

Silverlight adding textblocks dynamically

I know how to add text blocks dynamically using the following code:

TextBlock tb = new TextBlock();

tb.Width = 200; tb.Height = 60; tb.Text = "testing";

Canvas.SetLeft(tb, 10); Canvas.SetTop(tb, 10);

canvas.Children.Add(tb);

What I'd like to know is how to add them dynamically everytime the user clicks a button. So if the user presses the "Add Label" button 5 times, I want to dynamically add 5 textblocks to the canvas.

Upvotes: 0

Views: 1230

Answers (1)

X-Cubed
X-Cubed

Reputation: 1899

You can pretty much use the same code you've got there in your button event handler, although you'll need to change the SetLeft and SetTop calls to position the TextBlocks so that they are not on top of each other. I would suggest however that you use a StackPanel rather than a Canvas as it will manage the layout for you, by adding the buttons in a either a row or column depending on its Orientation property.

Upvotes: 1

Related Questions