How to: Add an Element Dynamically

This example shows how to add a Button element to a StackPanel by using the Add method of the Children property.

The following Extensible Application Markup Language (XAML) example creates a TabControl. When a user clicks the Add Control tab, a MouseLeftButtonDown event handler, AddButton, which is written in code, clears any existing controls and adds a new button.

<TabItem MouseLeftButtonUp="AddButton">
<TabItem.Header>Add Control</TabItem.Header>

		void AddButton(object sender, MouseButtonEventArgs e)
			btn = new Button();
			btn.Content = "New Button";

Private Sub AddButton(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
    btn = New Button()
    btn.Content = "New Button"
End Sub

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft