如何将TabControl的项目绑定到wpf中的observable集合? [英] How to bind items of a TabControl to an observable collection in wpf?

查看:281
本文介绍了如何将TabControl的项目绑定到wpf中的observable集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the simplest example of binding the items of a TabControl to an ObservableCollection?

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

Each tab's content will have unique data, and indeed this data will have observableCollections of its own bound to the items components.

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

Currently I have a user control, which I would like to set as the content of each tab as soon as it is created. I also need to dynamically set the datacontext of this new user control when the tab is created. So, essentially, I would like the tabcontrol's observablecollection contain modelviews that map to the data in each tab.

除此之外,我需要做所有这些而不违反MVVM在WPF!任何帮助?

On top of that, I need to do all this without violating MVVM in WPF! Any help?

非常感谢!

推荐答案

基本示例: / p>

Basic example :

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

这篇关于如何将TabControl的项目绑定到wpf中的observable集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆