protected override Windows.UI.Xaml.DataTemplate SelectTemplateCore(object item, Windows.UI.Xaml.DependencyObject container)
{
// grab the Type name. Type will be searched as Type:NAME as shown below
/*
<DataTemplate x:Key="Type:SampleDataItem">
<Grid HorizontalAlignment="Left" Width="250" Height="250">
<TextBlock Text="{Binding Title}" />
</Grid>
</DataTemplate>
*/
string key = item != null ? string.Format("Type:{0}", item.GetType().Name.Split('.').Last()) : DefaultTemplateKey;
DataTemplate dt = GetCachedDataTemplate(key);
try
{
if (dt != null) { return dt; }
// look at all parents (visual parents)
FrameworkElement fe = container as FrameworkElement;
while (fe != null)
{
dt = FindTemplate(fe, key);
if (dt != null) { return dt; }
// if you were to just look at logical parents,
// you'd find that there isn't a Parent for Items set
fe = VisualTreeHelper.GetParent(fe) as FrameworkElement;
}
dt = FindTemplate(null, key);
return dt;
}
finally
{
if (dt != null)
{
AddCachedDataTemplate(key, dt);
}
}
}