Bound TabControl Repeats Same Content Or Controls Across Tabs

300 views Asked by At

In the example below when data is bound to the tab control, it correctly has the tab headers per the data, but the tab content is always the same; and the last item bound to.

"Frank Wright"'s "Wright" is on every page.

What is wrong? One expects that there are different controls on every page, not one control shared or data repeated.

enter image description here

<Page.Resources>
    <model:People x:Key="People">
        <model:Person First="Joe"
                        Last="Smith"
                        Phone="303-555-5555" />
        <model:Person First="Jenny"
                        Last="Johnson"
                        Phone="720-867-5309" />
        <model:Person First="Frank"
                        Last="Wright"
                        Phone="202-555-5555" />
    </model:People>
</Page.Resources>
<TabControl ItemsSource="{StaticResource People}">
    <TabControl.ItemContainerStyle>
        <Style TargetType="TabItem">
            <Setter Property="Header"
                    Value="{Binding First}" />
            <Setter Property="Content">
                <Setter.Value>
                    <TextBox Text="{Binding Last}" />
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

public class People : List<Person> { }

public class Person
{
    public string First { get; set; }
    public string Last { get; set; }
    public string Phone { get; set; }
}
1

There are 1 answers

1
15ee8f99-57ff-4f92-890c-b56153 On BEST ANSWER

You want to set the ContentTemplate, not the Content:

<Setter Property="ContentTemplate">
    <Setter.Value>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBox Text="{Binding Last}" />
                <Label Content="{Binding Phone}" />
            </StackPanel>
        </DataTemplate>
    </Setter.Value>
</Setter>