public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (!(container is UIElement))
            {
                return(base.SelectTemplate(item, container));
            }


            SwagTemplateCollection templates        = new SwagTemplateCollection();
            IEnumerable            customSource     = GetCustomTemplates(container as UIElement);
            ICollectionView        customViewSource = CollectionViewSource.GetDefaultView(customSource);

            if (customViewSource != null)
            {
                foreach (SwagTemplate customTemplate in customViewSource)
                {
                    templates.Add(customTemplate);
                }
            }

            if (StaticTemplates != null)
            {
                foreach (SwagTemplate staticTemplate in StaticTemplates)
                {
                    templates.Add(staticTemplate);
                }
            }

            foreach (SwagTemplate template in templates)
            {
                if (!String.IsNullOrEmpty(ComparePath) && template.CompareValue != null)
                {
                    PropertyInfo propInfo = ReflectionHelper.PropertyInfoCollection[item.GetType()][ComparePath];
                    if (propInfo.CanRead)
                    {
                        Object targetValue = propInfo.GetValue(item);
                        if (targetValue.Equals(template.CompareValue))
                        {
                            return(template.DataTemplate);
                        }
                    }
                }

                if (template.Type != null && !String.IsNullOrEmpty(template.TypePath))
                {
                    PropertyInfo propInfo = ReflectionHelper.PropertyInfoCollection[item.GetType()][template.TypePath];
                    if (propInfo.CanRead)
                    {
                        Object targetType = propInfo.GetValue(item);
                        if (targetType != null && targetType.Equals(template.Type))
                        {
                            return(template.DataTemplate);
                        }
                    }
                }

                if (template.Type != null && template.Type.IsInstanceOfType(item))
                {
                    return(template.DataTemplate);
                }
            }

            SwagTemplate customDefaultTemplate = GetCustomDefaultTemplate(container as UIElement);

            if (customDefaultTemplate == null &&
                ((container is TreeViewItem) ||
                 (container is FrameworkElement && ((FrameworkElement)container).TemplatedParent is TreeViewItem)))
            {
                TreeView treeParent = container.TryFindParent <TreeView>();
                customDefaultTemplate = GetCustomDefaultTemplate(treeParent);
            }

            if (customDefaultTemplate != null)
            {
                return(customDefaultTemplate.DataTemplate);
            }

            //if (DefaultTemplate != null)
            //{
            //    return DefaultTemplate.DataTemplate;
            //}

            return(base.SelectTemplate(item, container));
        }
 public static void SetCustomTemplates(UIElement element, SwagTemplateCollection collection)
 {
     element.SetValue(CustomTemplatesProperty, collection);
 }