user1687824
user1687824

Reputation: 807

Databinding of a tabcontol -wpf

I want to design a tabcontrol. Each tabitem has a grid. Lets say grid is databound to list of object. Now how do i bind the tabcontrol to.

Below shown is the class

Class GridData
{
  String Name;
  string Id;
  string location
}

Class TabItems
{
  string tabName;
  List<GridData> ls;
 }

Whats the best way to implementing the databinding

Upvotes: 0

Views: 40

Answers (1)

dkozl
dkozl

Reputation: 33364

Say you have these 2 classes:

public class GridData
{
    public string Name { get; set; }
    public string Id { get; set; }
    public string Location { get; set; }
}

public class TabItems
{
    public string tabName { get; set; }
    public List<GridData> ls { get; set; }
}

and you set TabControl.ItemsSource to List<TabItems> then your XAML for that TabControl can look something like this:

<TabControl>
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding tabName}"/>
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding ls}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Path=Id, StringFormat='{}Id: {0}'}"/>
                            <TextBlock Text="{Binding Path=Name, StringFormat='{}Name: {0}'}"/>
                            <TextBlock Text="{Binding Path=Location, StringFormat='{}Location: {0}'}"/>
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

You specify 2 DataTemplates. TabControl.ItemTemplate for header and TabControl.ContentTemplate for content which in this example is ItemsControl to display ls

Upvotes: 1

Related Questions