none
ItemTemplateSelector throw exception RRS feed

  • 问题

  • i have simple page like below:

    <Page
        x:Class="Selector.MainPage"
        IsTabStop="false"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Selector"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
       <Page.Resources>
            <DataTemplate x:Key="dtCustomer">
                <TextBlock Text="{Binding Name}" FontSize="25"/>
            </DataTemplate>
            <DataTemplate x:Key="dtPrefCustomer">
                <TextBlock Foreground="Green" Text="{Binding Name}" FontSize="25" FontWeight="Bold" />
            </DataTemplate>
            <local:CustomerDataTemplateSelector  CustTemplate="{StaticResource dtCustomer}" x:Key="cdst" />
                
           
       </Page.Resources>
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
                <ListView Width="300" x:Name="list1" FontSize="30" ItemTemplateSelector="{StaticResource cdst}"/>
        </Grid>
        
    </Page>
    

    and the simple CustomerDataTemplateSelector.cs file 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    namespace Selector
    { // CustomerDataTemplateSelector.CustTemplate
        class CustomerDataTemplateSelector : DataTemplateSelector
        {
    
            
            public DataTemplate CustTemplate { get; set; }
            public DataTemplate PrefCustTemplate { get; set; }
    
           
            protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
            {
                Customer c = (Customer)item;
                
                
                // DataTemplate dt = c is PreferredCustomer ? this.PrefCustTemplate : this.CustTemplate;
                DataTemplate dt =this.CustTemplate;
                return dt;
            }
    
        }
    }
    

    but when run the app, it gets :WinRT information: Failed to assign to property 'Selector.CustomerDataTemplateSelector.CustTemplate'. [Line: 17 Position: 59]

    2012年9月5日 0:41

答案