View 集合中的 TabItems
我正在使用MVVM。我有一个选项卡控件。我将收集一些物品。我想将集合中的每个项目显示为选项卡项目。每个选项卡项中的视图都不同,并且可能有自己的视图模型。我该如何实现这一目标? 例如,我的收藏中有 3 件物品。 Tab 项模板包含一个 ItemControl。我现在想创建 3 个选项卡,并且每个选项卡内的 ItemControls 可能显示不同的视图。
我可以做的一种方法是为每个项目设置一个视图和视图模型。现在,根据某些条件,视图将显示不同的 UI 元素并表现不同。但恐怕这会让一段时间内的观点变得相当复杂。
编辑:下面的 Goblin 解决方案工作正常,但是当自定义样式应用于 TabControl 时我遇到了问题。
<Style x:Key="TabControlStyle" TargetType="{x:Type TabControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabControl">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/> <ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" Name="RowDefinition0" />
<RowDefinition Height="*" Name="RowDefinition1" />
</Grid.RowDefinitions>
<TabPanel Grid.Column="0" Grid.Row="0" />
<Border Grid.Column="0" Grid.Row="1">
<ContentPresenter Content="{TemplateBinding TabControl.SelectedContent}" ContentTemplate="{TemplateBinding TabControl.SelectedContentTemplate}" ContentStringFormat="{TemplateBinding TabControl.SelectedContentStringFormat}" ContentSource="SelectedContent" Name="PART_SelectedContentHost" Margin="{TemplateBinding Control.Padding}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
</Grid>
<ControlTemplate.Triggers>
编辑:通过将 ContentTemplateSelector 添加到上述 TabControl 样式中的 ContentPresenter 已解决此问题
I am using MVVM. I have a tab control. I will have a collection of items. I want to display each of this item in the collection as a tab item. The view in each tab item is different and may have its own viewmodel. How do I achieve this?
E.g. I have 3 items in the collection. The Tab item template contains an ItemControl. I would like to now have 3 Tabs created and the ItemControls inside each tabitem may be showing different views.
One way I could do is have a single view and viewmodel for each item. Now based on some condition the View will display different UI elements and behave differently. But I am afraid this will make the view quite complex over a period of time.
Edit: Goblin's solution below works fine but I have an issue when a custom style applied to TabControl.
<Style x:Key="TabControlStyle" TargetType="{x:Type TabControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabControl">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/> <ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" Name="RowDefinition0" />
<RowDefinition Height="*" Name="RowDefinition1" />
</Grid.RowDefinitions>
<TabPanel Grid.Column="0" Grid.Row="0" />
<Border Grid.Column="0" Grid.Row="1">
<ContentPresenter Content="{TemplateBinding TabControl.SelectedContent}" ContentTemplate="{TemplateBinding TabControl.SelectedContentTemplate}" ContentStringFormat="{TemplateBinding TabControl.SelectedContentStringFormat}" ContentSource="SelectedContent" Name="PART_SelectedContentHost" Margin="{TemplateBinding Control.Padding}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
</Grid>
<ControlTemplate.Triggers>
EDIT: This has been resolved by adding ContentTemplateSelector to the ContentPresenter in the above TabControl style
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为每个视图创建一个数据模板。实现一个 DataTemplateSelector 类,给定一个项目返回正确的数据模板。如果项目集合称为 Items,您的 xaml 将如下所示
Create a datatemplate for each view. Implement a DataTemplateSelector class which given an item returns the correct datatemplate. If the collection of items is called Items your xaml would look something like this
您尝试过使用 DataTemplateSelectors 吗?
基本上,您在主 ViewModel 中发布较小 ViewModel 的集合 - 然后在 DataTemplateSelector 中根据 ViewModel 的类型选择模板?
在代码隐藏中:
编辑:
ViewModel:
视图
Have you tried using DataTemplateSelectors?
Basically, you publish a collection of smaller ViewModels in your main ViewModel - then in the DataTemplateSelector choose your template based on the type of ViewModel?
In Code-behind:
EDIT:
ViewModels:
Views