AgostinoX
AgostinoX

Reputation: 7683

WPF TabControl: a dictionary for tab headers

I've got a TabControl that is bound to its viewmodel Items property:

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

This is the viewmodel class:

public class ViewModel
{
    public ObservableCollection<object> Items { get; } = new ObservableCollection<object>();
    public ViewModel()
    {
        Items.Add(new Person() { FirstName = "Alan", LastName = "Turing" });
        Items.Add(new Car() { ModelName = "Fiesta", Manifacturer = "Ford" });
    }

}

Items's type is ObservableCollection<object>; this is because each tab represents a different kind of object, in this example a Person and a Car.

Question

I want to bind the tab's header to a text that is not available as a property of the bound classes, let say a manually provided header.
For example, I would like the first tab to have the text "Person" as header and the second to have the text "Car" as header:

Person And Car As Tab's Headers

I thought of:

But how to write the binding expression then?

<TabControl ItemsSource="{Binding Items}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <Label Content="{Binding ????????}"/>
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

Even if possible, this solution seems to be not very 'binding-friendly'.

There seems to be two problems here: dynamically attach an information to an instance, and access it via binding.

Any ideas?

Postscript - the code

The code, for completeness. My classes:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Car
{
    public string ModelName { get; set; }
    public string Manifacturer { get; set; }
}

My DataTemplate, defined in the <Winodow.Resources> tag.

    <DataTemplate DataType="{x:Type local:Person}">
        <StackPanel>
            <Label Content="{Binding FirstName}"/>
            <Label Content="{Binding LastName}"/>
        </StackPanel>
    </DataTemplate>

    <DataTemplate DataType="{x:Type local:Car}">
        <StackPanel>
            <Label Content="{Binding Manifacturer}"/>
            <Label Content="{Binding ModelName}"/>
        </StackPanel>
    </DataTemplate>

Upvotes: 0

Views: 522

Answers (1)

dhilmathy
dhilmathy

Reputation: 2868

You can also override the ToString() method for your model classed and return your friendly name from there.

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public override string ToString()
    {
        return "Person";
    }
}

Template

<TabControl ItemsSource="{Binding Items}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}"/>
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

Upvotes: 1

Related Questions