示例#1
0
        private void ApplyMatchingStyles(TUIElement visualElement, StyleSheet styleSheet)
        {
            if (visualElement == null ||
                dependencyPropertyService.GetHandledCss(visualElement))
            {
                return;
            }

            var currentStyleSheet = dependencyPropertyService.GetStyleSheet(visualElement);

            if (currentStyleSheet != null &&
                currentStyleSheet != styleSheet)
            {
                return;
            }

            foreach (var child in treeNodeProvider.GetChildren(visualElement).ToList())
            {
                ApplyMatchingStyles(child as TUIElement, styleSheet);
            }

            var matchingStyles        = dependencyPropertyService.GetMatchingStyles(visualElement);
            var appliedMatchingStyles = dependencyPropertyService.GetAppliedMatchingStyles(visualElement);

            if (matchingStyles == appliedMatchingStyles ||
                (
                    matchingStyles != null &&
                    appliedMatchingStyles != null &&
                    matchingStyles.SequenceEqual(appliedMatchingStyles)
                ))
            {
                return;
            }

            object styleToApply = null;

            if (matchingStyles?.Length == 1)
            {
                if (applicationResourcesService.Contains(matchingStyles[0]) == true)
                {
                    styleToApply = applicationResourcesService.GetResource(matchingStyles[0]);
                }

                if (styleToApply != null)
                {
                    nativeStyleService.SetStyle(visualElement, (TStyle)styleToApply);
                }
            }
            else if (matchingStyles?.Length > 1)
            {
                var dict         = new Dictionary <TDependencyProperty, object>();
                var listTriggers = new List <TDependencyObject>();

                foreach (var matchingStyle in matchingStyles)
                {
                    object s = null;
                    if (applicationResourcesService.Contains(matchingStyle) == true)
                    {
                        s = applicationResourcesService.GetResource(matchingStyle);
                    }

                    var subDict = nativeStyleService.GetStyleAsDictionary(s as TStyle);

                    if (subDict != null)
                    {
                        foreach (var i in subDict)
                        {
                            dict[i.Key] = i.Value;
                        }

                        var triggers = nativeStyleService.GetTriggersAsList(s as TStyle);
                        listTriggers.AddRange(triggers);
                    }
                }

                if (dict.Keys.Count > 0)
                {
                    styleToApply = nativeStyleService.CreateFrom(dict, listTriggers, visualElement.GetType());
                }

                if (styleToApply != null)
                {
                    nativeStyleService.SetStyle(visualElement, (TStyle)styleToApply);
                }
            }

            dependencyPropertyService.SetHandledCss(visualElement, true);
            dependencyPropertyService.SetAppliedMatchingStyles(visualElement, matchingStyles);

            // Debug.WriteLine($"Applying: {string.Join(", ", dependencyPropertyService.GetMatchingStyles(visualElement) ?? new string[0])}");
        }