Exemple #1
0
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            ValidationHelper.NotNull(value, "value");
            ValidationHelper.OfType(value, "value", typeof(ThemeDictionaryBase));
            Util.EnsureNotNull <object>();
            Util.EnsureOfType <object>(typeof(System.Windows.ResourceDictionary));
            if (destinationType != typeof(System.Windows.ResourceDictionary))
            {
                return(base.ConvertTo(context, culture, value, destinationType));
            }
            var themeDictionaryBase = (ThemeDictionaryBase)value;
            var result = new System.Windows.ResourceDictionary {
                Source = Manager.ResourcesUri
            };

            System.Windows.ResourceDictionary resources;
            switch (themeDictionaryBase.Source)
            {
            case ThemeResources.Light:
                resources = new System.Windows.ResourceDictionary {
                    Source = Manager.LightBrushesDictionaryUri
                };
                result.SafeSet(resources);
                break;

            case ThemeResources.LightGray:
                resources = new System.Windows.ResourceDictionary {
                    Source = Manager.LightGrayBrushesDictionaryUri
                };
                result.SafeSet(resources);
                break;

            case ThemeResources.DarkGray:
                resources = new System.Windows.ResourceDictionary {
                    Source = Manager.DarkGrayBrushesDictionaryUri
                };
                result.SafeSet(resources);
                break;

            case ThemeResources.Dark:
                resources = new System.Windows.ResourceDictionary {
                    Source = Manager.DarkBrushesDictionaryUri
                };
                result.SafeSet(resources);
                break;

            case ThemeResources.Inherited:
                var themeDictionary = themeDictionaryBase as ThemeDictionary;
                if (themeDictionary == null)
                {
                    throw new InvalidOperationException("Dictionary must be of type ThemeDictionary");
                }
                if (themeDictionary.Control == null || !themeDictionary.Control.IsAlive)
                {
                    throw new InvalidOperationException("ThemeDictionary must be assigned with control before to be converted to System.Windows.ResourceDictionary");
                }
                var control = (FrameworkElement)themeDictionary.Control.Target;
                foreach (var resourceKey in themeDictionary.Keys)
                {
                    result.SafeSet(resourceKey, control.FindResource(resourceKey));
                }
                break;
            }
            result.SafeSet <ThemeResource, object>(themeDictionaryBase);
            return(result);
        }