Example #1
0
        public static bool TryGetStyleItem(FrameworkElement owner, Style style, out StyleItem styleItem)
        {
            styleItem = null;
            int globalIndex = GetGlobalIndex(style);

            foreach (var resource in ResourceHelper.GetResourcesRecursively<Style>(owner))
            {
                var resourceStyle = resource.Value as Style;
                if (resourceStyle != null && globalIndex == GetGlobalIndex(resourceStyle))
                {
                    var fullLocation = GetSource(resource.Owner);
                    string location = fullLocation.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
                    styleItem = new StyleItem(style, owner, GetKeyString(resource.Key), location, StyleScope.Local) { FullLocation = fullLocation };
                    return true;
                }
            }

            foreach (var resource in ResourceHelper.GetResourcesRecursively<Style>(Application.Current.Resources))
            {
                var resourceStyle = resource.Value as Style;
                if (resourceStyle != null && globalIndex == GetGlobalIndex(resourceStyle))
                {
                    // explicit resource key
                    string fullLocation = GetSource(resource.Owner) ?? "App.xml";
                    string location = fullLocation.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();

                    styleItem = new StyleItem(style, owner, GetKeyString(resource.Key), location, StyleScope.Application) { FullLocation = fullLocation };
                    return true;
                }
            }

            return false;
        }
Example #2
0
        public static bool TryGetStyleItem(FrameworkElement owner, Style style, out StyleItem styleItem)
        {
            styleItem = null;
            int globalIndex = GetGlobalIndex(style);

            foreach (var resource in ResourceHelper.GetResourcesRecursively <Style>(owner))
            {
                var resourceStyle = resource.Value as Style;
                if (resourceStyle != null && globalIndex == GetGlobalIndex(resourceStyle))
                {
                    var    fullLocation = GetSource(resource.Owner);
                    string location     = fullLocation.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
                    styleItem = new StyleItem(style, owner, GetKeyString(resource.Key), location, StyleScope.Local)
                    {
                        FullLocation = fullLocation
                    };
                    return(true);
                }
            }

            foreach (var resource in ResourceHelper.GetResourcesRecursively <Style>(Application.Current.Resources))
            {
                var resourceStyle = resource.Value as Style;
                if (resourceStyle != null && globalIndex == GetGlobalIndex(resourceStyle))
                {
                    // explicit resource key
                    string fullLocation = GetSource(resource.Owner) ?? "App.xml";
                    string location     = fullLocation.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();

                    styleItem = new StyleItem(style, owner, GetKeyString(resource.Key), location, StyleScope.Application)
                    {
                        FullLocation = fullLocation
                    };
                    return(true);
                }
            }

            return(false);
        }
        private void LoadStyles()
        {
            var style = Property.GetValue(Instance) as Style;
            var fe = Instance as FrameworkElement;
            if (style != null && fe != null)
            {
                // Add other applicable styles
                var targetType = fe.GetType();
                foreach (var resource in ResourceHelper.GetResourcesRecursivelyIncludingApp<Style>(fe))
                {
                    var styleResource = resource.Value as Style;
                    if (styleResource != null && styleResource.TargetType != null)
                    {
                        if ((targetType == styleResource.TargetType || targetType.IsSubclassOf(styleResource.TargetType)))
                        {
                            var resourceStyleItem = new StyleItem(styleResource, fe, StyleHelper.GetKeyString(resource.Key),
                                                                  StyleHelper.GetSource(resource.Owner), StyleScope.Local);

                            _styleItems.Add(resourceStyleItem);
                        }
                    }
                }

                // Add the current style
                StyleItem styleItem;
                if (StyleHelper.TryGetStyleItem(fe, style, out styleItem))
                {
                    if (!_styleItems.Contains(styleItem))
                    {
                        _styleItems.Add(styleItem);
                    }
                }

                Styles.Refresh();
                Styles.MoveCurrentTo(styleItem);
                Styles.CurrentChanged += OnStyleSelectionChanged;
            }
        }