绑定的 TabControl 跨选项卡重复相同的内容或控件

Bound TabControl Repeats Same Content Or Controls Across Tabs

在下面的示例中,当数据绑定到选项卡控件时,每个数据正确地具有选项卡 headers,但选项卡内容始终相同;和绑定到的最后一项。

"Frank Wright" 的 "Wright" 在每一页上。

怎么了?人们希望每一页都有不同的控件,而不是一个控件共享或数据重复。

<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; }
}

您想设置 ContentTemplate,而不是 Content:

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