WPF tab control

A WPF TabControl contains multiple items (TabItem), just like any tab control, and adding new tabs is quite easy. The following example shows a tab control with two tab items.

Empty tab with two items


    
		
			
			
		
	

However, when you try adding child controls to a TabItem you’ll notice that you can actually add one single child. The following XAML code yields an error.


    
	
		
			
			
		
		
	
   

error MC3089: The object ‘TabItem’ already has a child and cannot add ‘TextBox’. ‘TabItem’ can accept only one child. Line 9 Position 6.
The solution however is simple: use a grid or panel (Grid, UniformGrid, StackPanel or WrapPanel) to host multiple items. In the example bellow a Grid control was used to host the label and textbox.


    
	
		
			
				
				
			
		
		
	
   

The result can be seen in the following image:

Multiple items in a grid in a tab item

You can create all that from code. The equivalent in C# is the following:

public Window1()
{
        InitializeComponent();

        // create a new tab control
        TabControl tab = new TabControl();

        // create tab items and add them to the tab control
        TabItem item1 = new TabItem();
        item1.Header = "First";
        tab.Items.Add(item1);

        TabItem item2 = new TabItem();
        item2.Header = "Second";
        tab.Items.Add(item2);

        // create a new grid to host other controls
        Grid grid1 = new Grid();

        // create a label and add it to the grid childred
        Label label1 = new Label();
        label1.Margin = new Thickness(10, 10, 0, 0);
        label1.VerticalAlignment = VerticalAlignment.Top;
        label1.HorizontalAlignment = HorizontalAlignment.Left;
        label1.Width = 66;
        label1.Content = "Name";
        grid1.Children.Add(label1);

        // create a textbox and add it to the grid childred
        TextBox textBox1 = new TextBox();
        textBox1.Margin = new Thickness(80, 10, 10, 0);
        textBox1.VerticalAlignment = VerticalAlignment.Top;
        grid1.Children.Add(textBox1);

        // set the grid as the content of a tab item
        item1.Content = grid1;

        // add the tab control to the main grid of the window
        masterGrid.Children.Add(tab);
}

where masterGrid is the main grid in the window:

public partial class Window1 :
     System.Windows.Window, System.Windows.Markup.IComponentConnector {

     internal System.Windows.Controls.Grid masterGrid;



Hits for this post: 19580 .
Trackback

4 comments untill now

  1. Gravatar

    Thanks for the info =).

  2. Gravatar

    thanks man !

  3. Gravatar

    Question.

    i can make TabItems on the fly but how can I determin if a tabitem is or isn’t made?

    I would like to create an if statement before the tab is generated by ……

    If you know thanks a lot

  4. Gravatar

    Thank you!
    I’m starting to migrate to Windows Forms -> WPF and your post was very useful to me. Controls.Add was replaced with Children.Add, i never gonna imaginate this one.

    Sorry my bad English (I’m Brazilian), but thanks to help us with you know.

Add your comment now