Skip to content

Instantly share code, notes, and snippets.

@nanofi
Created September 21, 2012 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanofi/3759888 to your computer and use it in GitHub Desktop.
Save nanofi/3759888 to your computer and use it in GitHub Desktop.
DataTemplateSelector condition with item class name
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;
using Windows.UI.Xaml.Markup;
namespace Nanofi.Common{
[ContentProperty(Name = "Resources")]
public class ClassNameDataTemplateSelector : DataTemplateSelector
{
public ResourceDictionary Resources { get; set; }
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
if (Resources == null) return null;
string target = string.Format("{0}DataTemplate", item.GetType().Name);
return Resources.ContainsKey(target) ? Resources[target] as DataTemplate : null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment