Search code examples
c#wpftypeofdatatemplateselector

DataTemplateSelector getting typeof passed object


I have a object named "item" witch is passed to method from XAML

This is what I get on breakpoint:

base {System.Reflection.TypeInfo} = Name = "Country" Full/Name = "Playground.Domain.Country"}

I'm trying to find how can I found of which "Type" is the item by

public class EditorTemplateSelector : DataTemplateSelector
    {
      public override DataTemplate SelectTemplate(object item,
                                                  DependencyObject container)
      {
        DataTemplate template = null;
        var templateName = "NotFoundEditor";
        if (item != null)
        {
          FrameworkElement element = container as FrameworkElement;
          if (element != null)
          {
            if (item is City)
              templateName = "CityEditor";
            else if (item is Country)
              templateName = "CountryEditor";

            template = element.FindResource(templateName) as DataTemplate;
          }
        }
        return template;
      }

but with no luck.

The object item get its data from

public Type ModelType
{
  get { return typeof(T); }
}

Any suggestions?


Solution

  • In the light of your last edit:

    If "item" is a "System.Type" and not an instance of it, then use:

       if(item == typeof(City))