blu*_*bit 27 data-binding wpf tabcontrol observablecollection mvvm
将TabControl的项绑定到ObservableCollection的最简单示例是什么?
每个标签的内容都有唯一的数据,实际上这些数据会将自己的observableCollections绑定到items组件.
目前我有一个用户控件,我想在创建后立即将其设置为每个选项卡的内容.我还需要在创建选项卡时动态设置此新用户控件的datacontext.所以,基本上,我希望tabcontrol的observablecollection包含映射到每个选项卡中的数据的模型视图.
最重要的是,我需要在不违反WPF中的MVVM的情况下完成所有这些操作!有帮助吗?
非常感激!
Tho*_*que 38
基本示例:
<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>
Run Code Online (Sandbox Code Playgroud)