Home > Enterprise >  WPF ContentControl inside of TabControl does not show DataTemplates
WPF ContentControl inside of TabControl does not show DataTemplates

Time:12-06

I have a ListView which has TabControl which shows 2 type of views.

  <ListView x:Name="Devices" ItemsSource="{Binding Devices}">
            <ListView.Resources>
                <DataTemplate DataType="{x:Type app:DeviceViewModel}">
                    <Expander Header="{Binding Type}">
                        <StackPanel Orientation="Vertical">
                            <ContentControl>
                                <ContentControl.Resources>
                                    <DataTemplate DataType="{x:Type app:DeviceAViewModel}">
                                        <local:DeviceAView/>
                                    </DataTemplate>
                                    <DataTemplate DataType="{x:Type app:DeviceBViewModel}">
                                        <local:DeviceBView/>
                                    </DataTemplate>
                                </ContentControl.Resources>
                            </ContentControl>
                            <TabControl x:Name="Channel"  ItemsSource="{Binding Channels}" DisplayMemberPath="Index">
                                <TabControl.ContentTemplate>
                                    <DataTemplate>
                                        <ContentControl> 
                                            <ContentControl.Resources> <------ this part does't work
                                                <DataTemplate DataType="{x:Type app:ChannelAViewModel}">
                                                    <local:ChannelAView DataContext="{Binding}"/>
                                                </DataTemplate>
                                                <DataTemplate DataType="{x:Type app:ChannelBViewModel}">
                                                    <local:ChannelBView DataContext="{Binding}"/>
                                                </DataTemplate>
                                            </ContentControl.Resources>
                                        </ContentControl>
                                    </DataTemplate>
                                </TabControl.ContentTemplate>
                            </TabControl>
                        </StackPanel>
                    </Expander>
                </DataTemplate>
            </ListView.Resources>
        </ListView>

The content for each tab item is just empty instead of showing the matching channel view.

If I remove the whole <TabControl.ContentTemplate> Tree It does show the matching objects strings inside the tabitem content.

Any idea why it doesn't show the data templates?

Thank you.

CodePudding user response:

Try to Listview item template like this. like this:

<ListView x:Name="Devices" ItemsSource="{Binding Devices}">
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <StackPanel Orientation="Vertical">
                            <StackPanel Orientation="Vertical">
                                <ContentControl>
                                </ContentControl>
                                <TabControl x:Name="Channel"  ItemsSource="{Binding Channels}">
                                </TabControl>
                            </StackPanel>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

or this:

<ListView x:Name="Devices" ItemsSource="{Binding Devices}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <StackPanel Orientation="Vertical">
                    <ContentControl>
                    </ContentControl>
                    <TabControl x:Name="Channel"  ItemsSource="{Binding Channels}">
                    </TabControl>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

CodePudding user response:

Okay so I figured it out. Instead of using TabControl.ContentTemplate I placed the DataTemplates inside TabControl.Resources and it works fine.

 <TabControl x:Name="Channel"  ItemsSource="{Binding Channels}" DisplayMemberPath="Index">
                            <TabControl.Resources>
                                <DataTemplate DataType="{x:Type app:ChannelAViewModel}">
                                    <local:ChannelAView DataContext="{Binding}"/>
                                </DataTemplate>
                                <DataTemplate DataType="{x:Type app:ChannelBViewModel}">
                                    <local:ChannelBView DataContext="{Binding}"/>
                                </DataTemplate>
                            </TabControl.Resources>
                        </TabControl>
  • Related