Beispiel #1
0
        private static void Style_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var frameworkElement = (FrameworkElement)d;

            if (!frameworkElement.INTERNAL_DoNotApplyStyle)
            {
                Style newStyle = (Style)e.NewValue;
                Style oldStyle = (Style)e.OldValue;

                Dictionary <DependencyProperty, Setter> oldStyleDictionary = new Dictionary <DependencyProperty, Setter>();
                if (oldStyle != null)
                {
                    oldStyleDictionary = oldStyle.GetDictionaryOfSettersFromStyle();
                }
                Dictionary <DependencyProperty, Setter> newStyleDictionary = new Dictionary <DependencyProperty, Setter>();
                if (newStyle != null)
                {
                    newStyleDictionary = newStyle.GetDictionaryOfSettersFromStyle();
                }

#if PERFSTAT
                var t0 = Performance.now();
#endif
                frameworkElement.RecursivelyUnregisterFromStyleChangedEvents(oldStyle);
#if PERFSTAT
                Performance.Counter("RecursivelyUnregisterFromStyleChangedEvents", t0);
#endif

#if PERFSTAT
                var t1 = Performance.now();
#endif
                frameworkElement.RecursivelyRegisterToStyleChangedEvents(newStyle);
#if PERFSTAT
                Performance.Counter("RecursivelyRegisterToStyleChangedEvents", t1);
#endif

                foreach (Setter oldSetter in
#if BRIDGE
                         INTERNAL_BridgeWorkarounds.GetDictionaryValues_SimulatorCompatible(oldStyleDictionary)
#else
                         oldStyleDictionary.Values
#endif
                         )
                {
                    if (oldSetter.Property != null) // Note: it can be null for example in the XAML text editor during design time, because the "DependencyPropertyConverter" class returns "null".
                    {
                        if (!newStyleDictionary.ContainsKey(oldSetter.Property))
                        {
                            INTERNAL_PropertyStorage storage = INTERNAL_PropertyStore.GetStorage(d, oldSetter.Property, createAndSaveNewStorageIfNotExists: false); //the createAndSaveNewStorageIfNotExists's value should have no actual meaning here because the PropertyStorage should have been created when applying the style.
                            INTERNAL_PropertyStore.ResetLocalStyleValue(storage);
                        }
                    }

                    // Reset the information that tells which Style the Setter belongs to (this is used so that when the Value of the Setter changes, the Setter can notify its parent Style):
                    oldSetter.INTERNAL_ParentStyle = null;
                }

                foreach (Setter newSetter in
#if BRIDGE
                         INTERNAL_BridgeWorkarounds.GetDictionaryValues_SimulatorCompatible(newStyleDictionary)
#else
                         newStyleDictionary.Values
#endif
                         )
                {
                    if (newSetter.Property != null) // Note: it can be null for example in the XAML text editor during design time, because the "DependencyPropertyConverter" class returns "null".
                    {
                        if (!oldStyleDictionary.ContainsKey(newSetter.Property) || oldStyleDictionary[newSetter.Property] != newSetter.Value)
                        {
                            INTERNAL_PropertyStorage storage = INTERNAL_PropertyStore.GetStorage(frameworkElement, newSetter.Property, createAndSaveNewStorageIfNotExists: true); //the createAndSaveNewStorageIfNotExists's value should have no actual meaning here because the PropertyStorage should have been created when applying the style.
                            INTERNAL_PropertyStore.SetLocalStyleValue(storage, newSetter.Value);
                        }
                    }

                    // Tell the setter which Style it belongs to, so that when the Value of the Setter changes, it can notify the parent Style:
                    newSetter.INTERNAL_ParentStyle = newStyle;
                }
            }
        }