Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

WPF Dynamic TabControl – TabItems

, 16 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
What we want to achieve is to dynamicly insert TabItems, with dynamic header. Plus at any one time there may only be one TabItem, so we need to check if the tabControl contains the tabItem before creating. MainWindow.xaml <ListBoxItem x:Name="A1" Content="A1" MouseDoubleClick="NewTabItem" [...]

What we want to achieve is to dynamicly insert TabItems, with dynamic header.
Plus at any one time there may only be one TabItem, so we need to check if the tabControl contains the tabItem before creating.

MainWindow.xaml

<Window x:Class="HomeWork.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:HomeWork.UserControls"
        Title="MainWindow"
        Height="350"
        Width="525">
    <DockPanel>

        <ListBox DockPanel.Dock="Left" Margin="5" MinWidth="120">
            <ListBoxItem x:Name="A1" Content="A1" MouseDoubleClick="NewTabItem" />
            <ListBoxItem x:Name="A2" Content="A2" MouseDoubleClick="NewTabItem" />
        </ListBox>

        <TabControl x:Name="placeholder" Margin="5">
        </TabControl>        

    </DockPanel>
</Window>

MainWindow.cs

public void NewTabItem(object sender, RoutedEventArgs e)
{
  ListBoxItem source = sender as ListBoxItem;
  if (source != null)
  {
    bool tabExists = false;
    foreach (var tabItem in this.placeholder.Items)
    {
       if ((tabItem as TabItem).Name == source.Name)
       {
         tabExists = true;
        }
     }
     if (tabExists == false)
     {
       this.placeholder.SelectedIndex =
           this.placeholder.Items.Add(new TabItem
               { Header = source.Name, Name = source.Name });
     }
   }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

kribo
Software Developer
Belgium Belgium
Developer within C#, Dynamics NAV (Navision), Php environments.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150302.1 | Last Updated 16 Sep 2011
Article Copyright 2011 by kribo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid