示例#1
0
        public override Style SelectStyle(object item, DependencyObject container)
        {
            if (!(container is UIElement))
            {
                return(base.SelectStyle(item, container));
            }


            SwagStyleCollection styles           = new SwagStyleCollection();
            IEnumerable         customSource     = GetCustomStyles(container as UIElement);
            ICollectionView     customViewSource = CollectionViewSource.GetDefaultView(customSource);

            if (customViewSource != null)
            {
                foreach (SwagStyle customStyle in customViewSource)
                {
                    styles.Add(customStyle);
                }
            }

            if (StaticStyles != null)
            {
                foreach (SwagStyle staticStyle in StaticStyles)
                {
                    styles.Add(staticStyle);
                }
            }

            foreach (SwagStyle style in styles)
            {
                if (!String.IsNullOrEmpty(ComparePath) && style.CompareValue != null)
                {
                    PropertyInfo propInfo = ReflectionHelper.PropertyInfoCollection[item.GetType()][ComparePath];
                    if (propInfo.CanRead)
                    {
                        Object targetValue = propInfo.GetValue(item);
                        if (targetValue.Equals(style.CompareValue))
                        {
                            return(style.Style);
                        }
                    }
                }

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

                if (style.Type != null && style.Type.IsInstanceOfType(item))
                {
                    return(style.Style);
                }
            }

            SwagStyle customDefaultStyle = GetCustomDefaultStyle(container as UIElement);

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

            if (customDefaultStyle != null)
            {
                return(customDefaultStyle.Style);
            }

            if (DefaultStyle != null)
            {
                return(DefaultStyle.Style);
            }

            return(base.SelectStyle(item, container));
        }
示例#2
0
 public static void SetCustomDefaultStyle(UIElement element, SwagStyle style)
 {
     element.SetValue(CustomDefaultStyleProperty, style);
 }