1

I have a UWP page with a Pivot control, where ItemTemplateSelector is bounded to a DataTemplateSelector derived class. The code works as expected, and here is the XAML snippet

<Pivot IsHeaderItemsCarouselEnabled="False" 
ItemsSource ="{x:Bind MainPageViewModel.EditViewModels}"  
ItemTemplateSelector="{StaticResource DetailViewTemplateSelector}"  >
<Pivot.HeaderTemplate>
<DataTemplate >
<TextBlock Text="New Item"/>
</DataTemplate>
</Pivot.HeaderTemplate>
</Pivot>

I have a resource dictionary where the data template is defined as:

<DataTemplate x:Key="Tabela_DetailViewModel" >
<detailView:Tabela_View />
</DataTemplate>   

The code works as expected. As I add objects to the itemsource, new pivot items are created and their contents are rendered with the user control Tabela_View.

I changed the Pivot control to TabView from the Microsoft.Toolkit.Uwp.UI.Controls nampespace. After changing the control, when I open a new tab, the type name is rendered instead of the user control view. Here is XAML of the TabView:

<muxc:TabView x:Name="Tabs" Grid.Column="1" Grid.Row="0"   
ItemsSource ="{x:Bind MainPageViewModel.EditViewModels}"          
SelectedItem="{x:Bind MainPageViewModel.SelectedEntityViewModel,Mode=TwoWay}"
ItemTemplateSelector="{StaticResource DetailViewTemplateSelector}"    >
<muxc:TabView.ItemHeaderTemplate>
<DataTemplate >
<TextBlock Text="New Item"/>
</DataTemplate>
</muxc:TabView.ItemHeaderTemplate>
</muxc:TabView>

Here is a link for the project project link that simulates this error. Note that the commented pivot control snippet works as expected

Any help will be greatly appreciated. Regards, Saifi

saifi
  • 35
  • 4

1 Answers1

0

UWP TabView ItemTemplateSelector not working

I have tested ItemTemplateSelector and it works well in my side, please refer the following code to check if you have missed some key procedures.

Make data model

public class TabItem
{
    public ItemType Type { get; set; }
    public string Header { get; set; }
    public Microsoft.UI.Xaml.Controls.IconSource Icon { get; set; }
}
public enum ItemType { TypeA, TypeB };

Make TabViewItemTemplateSelector class that inherit DataTemplateSelector.

public class TabViewItemTemplateSelector : DataTemplateSelector
{
    public DataTemplate TemplateOne { get; set; }
    public DataTemplate TemplateTwo { get; set; }

    protected override DataTemplate SelectTemplateCore(object item)
    {
        var tabItem = (TabItem)item;
        return tabItem.Type == ItemType.TypeA ? TemplateOne : TemplateTwo;
    }
}

Xaml Code

<Page.Resources>
    <local:TabViewItemTemplateSelector
        x:Key="TabViewItemTemplateselector"
        TemplateOne="{StaticResource Templateone}"
        TemplateTwo="{StaticResource Templatetwo}"
        />
    <DataTemplate x:Key="Templateone" x:DataType="local:TabItem">
        <muxc:TabViewItem Header="{x:Bind Header}" IconSource="{x:Bind Icon}" >
            <detailview:Tabela_View/>
        </muxc:TabViewItem>
    </DataTemplate>
    <DataTemplate x:Key="Templatetwo" x:DataType="local:TabItem" >
        <muxc:TabViewItem Header="{x:Bind Header}" IconSource="{x:Bind Icon}" >
            <detailview:NormalPage/>
        </muxc:TabViewItem>
    </DataTemplate>
</Page.Resources>

Usage

<muxc:TabView
    x:Name="MyTabView"
    Grid.Row="0"
    Grid.Column="1"
    TabItemTemplateSelector="{StaticResource TabViewItemTemplateselector}"
    />


MyTabView.TabItemsSource = new List<TabItem>() {
    new TabItem(){ Header="One", Icon = new Microsoft.UI.Xaml.Controls.SymbolIconSource(){ Symbol= Symbol.DockBottom},Type=ItemType.TypeA },
     new TabItem(){ Header="Two",  Icon = new Microsoft.UI.Xaml.Controls.SymbolIconSource(){ Symbol= Symbol.Document},Type=ItemType.TypeB },
      new TabItem(){ Header="Three",  Icon = new Microsoft.UI.Xaml.Controls.SymbolIconSource(){ Symbol= Symbol.Accept},Type=ItemType.TypeA }
};
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • Thanks for responding. I appreciate a lot if you can check the kink of the sample I attached in my original post. In my solution, I did what you suggested in you answer. The difference is that I am using a merged resource dictionary that define the data templates that should be used by the template selector. In the template selector, I retrieve the template from resources according to view model type. In the sample, you can see how the same code will work with Pivot and not with Tabs. I appreciate a lot if you can take a look at the code in the sample and see if you can figure what is going on – saifi Jan 24 '20 at 09:36
  • One more point, I could not use x:DataType in resource dictionary. It generates a compile error [The XAML Binary Format (XBF) generator reported syntax error '0x09C4']. Note that missing x:DataType was not a problem for Pivot control – saifi Jan 24 '20 at 09:41
  • You could remove `x:DataType` and use binding scheme. – Nico Zhu Jan 28 '20 at 01:42