c# - 如何在 WPF 中使用带有动态 View 的选项卡控件创建动态选项卡

标签 c# wpf mvvm tabcontrol

我正在使用的软件是为一家打印公司服务的。我总共有 5 个不同的 View /VM,我可能会在 TabControl 内的单个 TabItem 中显示它们。我正在创建的 TabControlViewModel 上创建它们的列表。例如列表 - FormsViewModel、PlasticsViewModel、LabelsViewModel;此列表应生成 3 个包含其各自 View 的动态选项卡。最重要的是,我希望能够实际开发它,以便我从这个列表中获得不同的 View 作为第一个选项卡,然后最后一个选项卡也将具有与列表不同的 View 。基本上 2 个选项卡将围绕动态选项卡列表。这是到目前为止我一直在搞乱的代码。

 <UserControl.Resources>
    <ResourceDictionary>
        <DataTemplate DataType="{x:Type jobDetails:JobProductionAddOnViewModel}">
            <jobDetails:JobProductionAddOnView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type forms:FormsDetailViewModel}">
            <forms:FormsDetailView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type labels:LabelsDetailViewModel}">
            <labels:LabelsDetailView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type plastics:PlasticsDetailViewModel}">
            <plastics:PlasticsDetailView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type specialtyCoatings:SpecialtyCoatingsDetailViewModel}">
            <specialtyCoatings:SpecialtyCoatingsDetailView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type digitalLabels:DigitalLabelDetailViewModel}">
            <digitalLabels:DigitalLabelDetailView />
        </DataTemplate>
    </ResourceDictionary>
</UserControl.Resources>
<Grid>
    <TabControl ItemsSource="{Binding AdditionalDetailsViewModelList}"
                controls:TabControlHelper.IsUnderlined="True" 
                controls:TabControlHelper.Transition="Left"
                TabStripPlacement="Left"
                SelectedIndex="{Binding SelectedIndex}"
                ItemContainerStyle="{StaticResource ProductionTabItem}">
        <TabItem>
            <TabItem.Header>
                <Label Content="Primary"
                       Width="100"
                       HorizontalContentAlignment="Center"/>
            </TabItem.Header>
            <AdornerDecorator>
                <ContentControl Content="{Binding PrimaryDetailsViewModel}" />
            </AdornerDecorator>
        </TabItem>
        <!--I have tried adding an ItemsControl here, didn't work-->
        <!--I have also tried adding ItemsSource and binding it to the dynamic list-->
        <!--But can't figure out how to change the view based on the type of viewmodel like-->
        <!--you would with an itemscontrol-->
        <TabItem>
            <TabItem.Header>
                <Label Content="+"
                       Width="100"
                       HorizontalContentAlignment="Center"/>
            </TabItem.Header>
            <AdornerDecorator>
                <ContentControl Content="{Binding JobProductionAddOnViewModel}" />
            </AdornerDecorator>
        </TabItem>
    </TabControl>
</Grid>

最佳答案

这就是我找到的解决方案。我的大部分想法来自这篇文章 here . ViewModel 属性是我的 ViewModel 继承的称为 IBaseViewModel 的接口(interface)。

 <UserControl.Resources>
    <ResourceDictionary>
        <DataTemplate DataType="{x:Type jobDetails:JobProductionAddOnViewModel}">
            <jobDetails:JobProductionAddOnView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type forms:FormsDetailViewModel}">
            <forms:FormsDetailView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type labels:LabelsDetailViewModel}">
            <labels:LabelsDetailView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type plastics:PlasticsDetailViewModel}">
            <plastics:PlasticsDetailView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type specialtyCoatings:SpecialtyCoatingsDetailViewModel}">
            <specialtyCoatings:SpecialtyCoatingsDetailView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type digitalLabels:DigitalLabelDetailViewModel}">
            <digitalLabels:DigitalLabelDetailView />
        </DataTemplate>
    </ResourceDictionary>
</UserControl.Resources>
<Grid>
    <TabControl ItemsSource="{Binding TabItems}"
                controls:TabControlHelper.IsUnderlined="True" 
                controls:TabControlHelper.Transition="Left"
                TabStripPlacement="Left"
                SelectedIndex="{Binding SelectedIndex}"
                ItemContainerStyle="{StaticResource ProductionTabItem}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Header}"
                           Width="150"/>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <ContentControl Content="{Binding ViewModel}" />
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
</Grid>

关于c# - 如何在 WPF 中使用带有动态 View 的选项卡控件创建动态选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54329894/

相关文章:

c# - 根据百分比从 IEnumerable 中选择行

c# - 在 using 语句中修改值类型变量

c# - SetBinding() 和 Property={Binding Path} 的区别

c# - DisplayMemberPath 行为

c# - WPF MVVM 导航技术

c# - 计算不同年份不同日期的平均值

wpf - WPF 中的 ListBox 与 ViewModel 绑定(bind)

java - 在屏幕中的不同 Fragment 之间共享一个 ViewModel 实例

c# - 将 UserControl 中元素的属性绑定(bind)到 MyViewModel.cs 中的属性

c# - ASP.NET - 是否可以将用户代理放入对象中?