27

将 TabControl 的项目绑定到 ObservableCollection 的最简单示例是什么?

每个选项卡的内容都将具有唯一的数据,并且实际上这些数据将具有自己绑定到项目组件的 observableCollections。

目前我有一个用户控件,我想在创建后立即将其设置为每个选项卡的内容。我还需要在创建选项卡时动态设置这个新用户控件的数据上下文。因此,基本上,我希望 tabcontrol 的 observablecollection 包含映射到每个选项卡中的数据的模型视图。

最重要的是,我需要在不违反 WPF 中的 MVVM 的情况下完成这一切!有什么帮助吗?

非常感激!

4

1 回答 1

40

基本示例:

<Window.Resources>

    <DataTemplate x:Key="templateForTheContent" DataType="{x:Type vm:TheViewModelType}">
        <v:YourUserControl/>
    </DataTemplate>

    <DataTemplate x:Key="templateForTheHeader" DataType="{x:Type vm:TheViewModelType}">
        <TextBlock Text="{Binding ThePropertyToDisplayInTheHeader}"/>
    </DataTemplate>

</Window.Resources>

...

<TabControl ItemsSource="{Binding YourCollection}"
            ContentTemplate="{StaticResource templateForTheContent}"
            ItemTemplate="{StaticResource templateForTheHeader}">
</TabControl>
于 2009-08-11T16:05:38.190 回答