== Creating Controls Dynamically == Source: http://stackoverflow.com/questions/1136701/how-to-access-button-programatically-if-the-button-is-inside-using-c private void listBox1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { //Get selected item ListBoxItem item = (ListBoxItem)listBox1.SelectedItem; string itemText = item.Content.ToString(); //Check if item is already added foreach (TabItem tItem in tabControl1.Items) { if ((string)tItem.Tag == itemText) { //Item already added, just activate the tab tabControl1.SelectedItem = tItem; return; } } //Build new tab item TabItem tabItem = new TabItem(); tabItem.Tag = itemText; //First build the Header content Label label = new Label(); Button button = new Button(); label.Content = itemText; button.Content = "X"; button.Height = 20; button.Width = 20; button.FontWeight = FontWeights.Bold; button.Click += CloseTabButton_Click; //Attach the event click method button.Tag = tabItem; //Reference to the tab item to close in CloseTabButton_Click StackPanel panel = new StackPanel(); panel.Orientation = Orientation.Horizontal; panel.Children.Add(label); panel.Children.Add(button); tabItem.Header = panel; //Then build the actual tab content //If you need only the ListView in here, you don't need a grid ListView listView = new ListView(); //TODO: Populate the listView with what you need tabItem.Content = listView; //Add the finished tabItem to your TabControl tabControl1.Items.Add(tabItem); tabControl1.SelectedItem = tabItem; //Activate the tab } private void CloseTabButton_Click(object sender, RoutedEventArgs e) { //The tab item was set to button.Tag when it was added Button button = (Button)sender; TabItem tabItem = (TabItem)button.Tag; if (tabItem != null) { button.Click -= CloseTabButton_Click; //Detach event handler to prevent memory leak tabControl1.Items.Remove(tabItem); } } == Creating a Window == var window = new Window(); var stackPanel = new StackPanel { Orientation = Orientation.Vertical }; stackPanel.Children.Add(new Label { Content = "Label" }); stackPanel.Children.Add(new Button { Content = "Button" }); window.Content = stackPanel;