public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item == null)
            {
                return(base.SelectTemplate(item, container));
            }

            DataTemplate template = null;

            if ((item is byte[]) || (item is System.Drawing.Image))
            {
                bool useImageTemplate = false;

                try
                {
                    var converter = new ImageConverter();
                    useImageTemplate = (converter.Convert(item, typeof(ImageSource), null, CultureInfo.CurrentCulture) != null);
                }
                catch (NotSupportedException)
                {
                    //suppress the exception, the byte[] is not an image. convertedValue will remain null
                }

                if (useImageTemplate)
                {
                    template = GenericContentTemplateSelector.GetImageTemplate(container);
                }
            }
            else if (item is ImageSource)
            {
                template = GenericContentTemplateSelector.GetImageTemplate(container);
            }
            else if (item is bool)
            {
                template = GenericContentTemplateSelector.BoolTemplate;
            }

            if (template == null)
            {
                template = GenericContentTemplateSelector.GetCommonTemplate(item, container);
            }

            if (template != null)
            {
                return(template);
            }

            return(base.SelectTemplate(item, container));
        }
        private static DataTemplate GetCommonTemplate(object item, DependencyObject container)
        {
            Debug.Assert(item != null);

            var itemType = item.GetType();

            // Do not provide a template for data types that are already optimized by the framework or
            // for data types that have a default template.
            if (GenericContentTemplateSelector.IsTypeOptimized(itemType) ||
                GenericContentTemplateSelector.HasImplicitTemplate(item, itemType, container))
            {
                return(null);
            }

            return(GenericContentTemplateSelector.GetDefaultTemplate(itemType));
        }
        private static DataTemplate GetDefaultTemplate(Type type)
        {
            Debug.Assert(type != null);

            var converter = TypeDescriptor.GetConverter(type);

            if ((converter == null) || (!converter.CanConvertTo(typeof(string))))
            {
                return(GenericContentTemplateSelector.CommonTemplate);
            }

            var templates = GenericContentTemplateSelector.DefaultTemplates;

            lock ((( ICollection )templates).SyncRoot)
            {
                for (int i = templates.Count - 1; i >= 0; i--)
                {
                    var target         = templates[i];
                    var targetTemplate = target.Value.Target as DataTemplate;

                    // We have found the desired template.
                    if (target.Key == type)
                    {
                        if (targetTemplate != null)
                        {
                            return(targetTemplate);
                        }

                        // Unfortunately, the template has been garbage collected.
                        templates.RemoveAt(i);
                        break;
                    }
                }

                templates.TrimExcess();

                var template = GenericContentTemplateSelector.CreateDefaultTemplate(type, converter);

                templates.Add(new KeyValuePair <Type, WeakReference>(type, new WeakReference(template)));

                return(template);
            }
        }
        private static bool HasImplicitTemplate(object item, Type type, DependencyObject container)
        {
            Debug.Assert(type != null);

            var finder = GenericContentTemplateSelector.FindTemplateResource;

            if (finder == null)
            {
                finder = GenericContentTemplateSelector.GetFindTemplateResourceInternal();

                if (finder == null)
                {
                    finder = GenericContentTemplateSelector.FindTemplateResourceFallback;
                }

                GenericContentTemplateSelector.FindTemplateResource = finder;
            }

            Debug.Assert(finder != null);

            return(finder.Invoke(container, item, type) != null);
        }