C# WP 8.1:DataTemplateSelector isn';行不通

C# WP 8.1:DataTemplateSelector isn';行不通,c#,xaml,listview,datatemplate,windows-phone-8.1,C#,Xaml,Listview,Datatemplate,Windows Phone 8.1,我有一个Windows Phone 8.1 Pivot应用程序项目 我试图在xaml列表视图中创建DataTemplate的选择,但失败了。我可以运行该应用程序,但遇到了一个中断:global::System.Diagnostics.Debugger.break()。 下面是一些代码 我的DataTemplateSelector namespace DMI { public class ExploreTemplateSelector : DataTemplateSelector

我有一个Windows Phone 8.1 Pivot应用程序项目

我试图在xaml列表视图中创建DataTemplate的选择,但失败了。我可以运行该应用程序,但遇到了一个中断:
global::System.Diagnostics.Debugger.break()。

下面是一些代码

我的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;
        }
    }
}
我的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>
错误2:

Property 'VisualTree' does not support values of type 'ExploreTemplateSelector'.    
PivotPage.xaml  49
而且在我的xaml中,
似乎连识别都没有。 有什么想法吗


谢谢

不能在DataTemplate中使用DataTemplateSelector。您不必设置ListView的
ItemTemplate
,而是可以设置其
ItemTemplateSelector
属性,最好是通过StaticResource表达式:

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

...

无法将模板选择器指定给
ItemTemplate
属性。是否有
ItemTemplateDataSelector
属性?谢谢Clemens!它起作用了!我在这里遵循了一个教程:但我似乎错过了他实现datatemplateselector的方式。。。无论如何,谢谢:)@ApheX,你在链接中给出的教程运行良好。
<Page.Resources>
    <local:ExploreTemplateSelector x:Key="ExploreTemplateSelector">
        ...
    </<local:ExploreTemplateSelector>
</Page.Resources>
...
<ListView ... ItemTemplateSelector="{StaticResource ExploreTemplateSelector}">
    ...
</ListView>