How can I bind a List collection to TabControl headers in WPF?

43,281

Solution 1

just bind your List to your TabControl as ItemsSource, e.g.

<TabControl ItemsSource="{Binding Customers}"/>

this will give you a tab for each object in customer.

Solution 2

Here ist what I would do

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //create all 
        var customers = new List<Customer>{
            new Customer {FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23},
            new Customer {FirstName = "Jane", LastName = "Smith", NumberOfContracts = 23},
            new Customer {FirstName = "John", LastName = "Tester", NumberOfContracts = 23}};

        //show 
        TheTabControl.ItemsSource = customers;
        TheTabControl.SelectedIndex = 0;
    }


public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int NumberOfContracts { get; set; }
}

And on the XAML side

<TabControl x:Name="TheTabControl">            
    <TabControl.ItemTemplate>
        <DataTemplate>                    
            <TextBlock>                            
                <TextBlock Text="{Binding FirstName}"/> <TextBlock Text="{Binding LastName}"/>
            </TextBlock>                        
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <TextBlock>                            
                This is <TextBlock Text="{Binding FirstName}"/> <TextBlock Text="{Binding LastName}"/>
            </TextBlock>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

Solution 3

Your answer can be found here.

http://www.codeplex.com/smartclient/Thread/View.aspx?ThreadId=31821

Notice how he sets the ContentTemplate as well as the ItemTemplate...you almost had it!

Share:
43,281

Related videos on Youtube

Angry Dan
Author by

Angry Dan

web/software developer, .NET, C#, WPF, PHP, software trainer, English teacher, have philosophy degree, love languages, run marathons my tweets: http://www.twitter.com/edward_tanguay my runs: http://www.tanguay.info/run my code: http://www.tanguay.info/web my publications: PHP 5.3 training video (8 hours, video2brain) my projects: http://www.tanguay.info

Updated on July 09, 2022

Comments

  • Angry Dan
    Angry Dan almost 2 years

    I can get data into my TabControl but the headers have frames around them and I can't slick from tab to tab.

    What am I doing wrong with the XAML binding syntax on this TabControl?

    XAML:

    <StackPanel>
        <TabControl x:Name="TheTabControl">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TabItem Header="{Binding LastName}">
                        <StackPanel Margin="10" Orientation="Horizontal">
                            <TextBlock Text="{Binding FirstName}"/>
                            <TextBlock Text=" "/>
                            <TextBlock Text="{Binding LastName}"/>
                        </StackPanel>
                    </TabItem>
                </DataTemplate>                
            </TabControl.ItemTemplate>
        </TabControl>
    
        <TabControl>
            <TabItem Header="Tab1">
                <TextBlock Text="This is a test of tab 1"/>
            </TabItem>
            <TabItem Header="Tab2">
                <TextBlock Text="This is a test of tab 2"/>
            </TabItem>
        </TabControl>
    
    </StackPanel>  
    

    code behind:

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
    
            //create all
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer { FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23 });
            customers.Add(new Customer { FirstName = "Jane", LastName = "Smith", NumberOfContracts = 23 });
            customers.Add(new Customer { FirstName = "John", LastName = "Tester", NumberOfContracts = 23 });
    
            //show
            TheListBox.ItemsSource = customers;
    
        }
    }
    
    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int NumberOfContracts { get; set; }
    }
    
    • Robert P
      Robert P over 13 years
      See Christof's answer ( stackoverflow.com/questions/589802/… ) for the XAML to make this work - you need a TabControl.ContentTemplate block for the content since the TabControl.ItemTemplate is ONLY for the header part of the tab.
  • Angry Dan
    Angry Dan over 15 years
    Thanks, that got me further, but I now I can't click from tab to tab and the header texts have frames around them. I posted the new code above, what needs to change so I can just bind data into the header and the content of the tabs?
  • dhara tcrails
    dhara tcrails over 13 years
    Ah!, ContentTemplate. Thanks!
  • LineloDude
    LineloDude almost 10 years
    This is my problem -- everything works except can't select tabs by clicking on the header text, which makes the tabcontrol nearly useless.