Cartesius00
Cartesius00

Reputation: 24364

Binding TabControl to collection of ViewModels

how to bind TabControl to ObservableCollection of ViewModels? I can't write DataTemplates... more precisely, how to specify what's in the title and what's in the body of each TabItem. I would appreciate mini XAML example, please.

Upvotes: 2

Views: 329

Answers (1)

brunnerh
brunnerh

Reputation: 184441

Header is specified by ItemTemplate, body is specified by ContentTemplate.

e.g.

<TabControl ItemsSource="{Binding DpData}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" />
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Occupation}" />
                <TextBlock Text="{Binding Status}" />
                <TextBlock Text="{Binding IsActive}" />
            </StackPanel>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

If you have trouble with bindings and datatemplates read the overviews first:

Data Binding Overview
Data Templating Overview

Upvotes: 3

Related Questions