ApheX
ApheX

Reputation: 685

WP 8.1: DataTemplateSelector isn't working

I've got a Windows Phone 8.1 Pivot App project.

I'm trying to create DataTemplate's selection in a xaml listview but it fail. I can run the app but i got a break: global::System.Diagnostics.Debugger.Break().

Here is some code.

My DataTemplateSelector

namespace DMI
{
    public class ExploreTemplateSelector : DataTemplateSelector
    {
        public DataTemplate FolderTemplate { get; set; }
        public DataTemplate DocumentTemplate { get; set; }

        protected override DataTemplate SelectTemplateCore(object item)
        {
            if (item.GetType() == typeof(FolderExplore))
                return FolderTemplate;
            else 
                return DocumentTemplate;
        }
    }
}

My Pivot XAML

<Page
    x:Class="DMI.PivotPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:DMI"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:DMI.Data"
    mc:Ignorable="d"
    Background="#424242">
    <!--Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">-->
    <Page.Transitions>
        <TransitionCollection>
            <NavigationThemeTransition>
                <NavigationThemeTransition.DefaultNavigationTransitionInfo>
                    <CommonNavigationTransitionInfo IsStaggeringEnabled="True"/>
                </NavigationThemeTransition.DefaultNavigationTransitionInfo>
            </NavigationThemeTransition>
        </TransitionCollection>
    </Page.Transitions>

    <Grid>
        <Image Margin="0,2,1,523" Source="Assets/WideLogo.scale-240.png" Stretch="Fill" RenderTransformOrigin="0.5,0.5">
            <Image.RenderTransform>
                <CompositeTransform ScaleX="-1"/>
            </Image.RenderTransform>
        </Image>
        <Pivot 
            x:Uid="Pivot"
            x:Name="pivot" 
            CommonNavigationTransitionInfo.IsStaggerElement="True" 
            Margin="0,20,0,0"
            SelectionChanged="Pivot_SelectionChanged">

            <PivotItem
                x:Uid="PivotItem1"
                x:Name="PivotItem1"
                Header="Dashboard"
                CommonNavigationTransitionInfo.IsStaggerElement="True">

                <ListView
                    x:Name="ListDocuments"
                    IsItemClickEnabled="True"
                    ItemClick="ItemView_ItemClick"
                    ContinuumNavigationTransitionInfo.ExitElementContainer="True">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <local:ExploreTemplateSelector>
                                <local:ExploreTemplateSelector.FolderTemplate>
                                    <DataTemplate>
                                        <Canvas Background="Transparent">
                                            <Image Source="Assets/StoreLogo.scale-240.png"  VerticalAlignment="Stretch" Margin="0,0,0,0" Width="80" Height="30" Stretch="Fill" />
                                            <TextBlock TextWrapping="Wrap" Text="{Binding folderName}" FontSize="20" FontWeight="Normal" VerticalAlignment="Center" Margin="80,0,0,0" />
                                        </Canvas>
                                    </DataTemplate>
                                </local:ExploreTemplateSelector.FolderTemplate>
                                <local:ExploreTemplateSelector.DocumentTemplate>
                                    <DataTemplate>
                                        <Canvas Background="Transparent">
                                            <Image Source="Assets/StoreLogo.scale-240.png"  VerticalAlignment="Stretch" Margin="0,0,0,0" Width="80" Height="30" Stretch="Fill" />
                                            <TextBlock TextWrapping="Wrap" Text="{Binding documentName}" FontSize="20" FontWeight="Normal" VerticalAlignment="Center" Margin="80,0,0,0" />
                                        </Canvas>
                                    </DataTemplate>
                                </local:ExploreTemplateSelector.DocumentTemplate>
                            </local:ExploreTemplateSelector>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </PivotItem>

            <PivotItem
                x:Uid="PivotItem2"
                x:Name="PivotItem2"/>

            <PivotItem
                x:Uid="PivotItem3"
                x:Name="PivotItem3"/>

            <PivotItem
                x:Uid="PivotItem4"
                x:Name="PivotItem4"/>
        </Pivot>
    </Grid>
</Page>

I've got two erros in the xaml: Error 1:

The specified value cannot be assigned. The following type was expected: "DependencyObject".    
PivotPage.xaml  49

Error 2:

Property 'VisualTree' does not support values of type 'ExploreTemplateSelector'.    
PivotPage.xaml  49

And it seems that <local:ExploreTemplateSelector> is not even recognized in my xaml. Any ideas?

Thanks!

Upvotes: 1

Views: 1713

Answers (1)

Clemens
Clemens

Reputation: 128062

You can't use a DataTemplateSelector inside a DataTemplate. Instead of setting the ListView's ItemTemplate you would set its ItemTemplateSelector property, preferrably by a StaticResource expression:

<Page.Resources>
    <local:ExploreTemplateSelector x:Key="ExploreTemplateSelector">
        ...
    </<local:ExploreTemplateSelector>
</Page.Resources>
...
<ListView ... ItemTemplateSelector="{StaticResource ExploreTemplateSelector}">
    ...
</ListView>

Upvotes: 2

Related Questions