Как создать динамические вкладки с помощью вкладки управления с динамическими представлениями в WPF - PullRequest
0 голосов
/ 23 января 2019

Программное обеспечение, над которым я работаю, предназначено для типографии.У меня есть в общей сложности 5 разных видов / виртуальных машин, которые я мог бы отображать в одном элементе TabItem внутри TabControl.Я создаю их список на TabControlViewModel, который я создал.Так, например, List - FormsViewModel, PlasticsViewModel, LabelsViewModel;Этот список должен создать 3 динамические вкладки, содержащие их соответствующие представления.Кроме того, я надеялся, что смогу разработать его так, чтобы у меня было другое представление из этого списка в качестве первой вкладки, тогда как последняя вкладка также будет иметь другое представление из списка.В основном 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>

1 Ответ

0 голосов
/ 23 января 2019

Это было решение, к которому я пришел.И большую часть своей идеи я получил из этого поста здесь .Свойство ViewModel - это интерфейс, который мои наследующие ViewModels называют IBaseViewModel.

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