Dynamically Creating Tabs in WPF

I've been experimenting with dynamically created tabs in WPF.  The idea is that the user selects a menu item and a tab is created and populated with a user control.  Like so many things, WPF makes this easy once you figure out how to do it.  The thing I struggled with the most was getting the user control to display when the first tab was created.  Below is a function that takes a user control and tab name, creates a tab, and shows it to the user.

/// <summary> /// Create a new tab, populate it with the passed in user control, /// and display it to the user. /// </summary> /// <param name="detail">UserControl to populate the tab with</param> /// <param name="name">Name of the tab</param> public void AddDetail(UserControl detail, string name) { // make sure the passed in arguments are good Debug.Assert(detail != null, "UserControl detail is null"); Debug.Assert(name != null, "string name is null"); // locate the TabControl that the tab will be added to TabControl itemsTab = (TabControl) this.FindName("ItemsTab"); Debug.Assert(itemsTab != null, "can't find ItemsTab"); // create and populate the new tab and add it to the tab control TabItem newTab = new TabItem(); newTab.Content = detail; newTab.Header = name; itemsTab.Items.Add(newTab); // display the new tab to the user; if this line is missing // you get a blank tab itemsTab.SelectedItem = newTab; }

Print | posted @ Saturday, October 27, 2007 6:56 AM

Comments on this entry:

No comments posted yet.

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 5 and 3 and type the answer here: