简体   繁体   English

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

[英]Bound TabControl Repeats Same Content Or Controls Across Tabs

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. 人们期望每个页面都有不同的控件,而不是一个控件共享或重复数据。

在此输入图像描述

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

You want to set the ContentTemplate , not the Content : 您想要设置ContentTemplate ,而不是Content

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM