/// <summary> /// This Style is now immutable /// </summary> public void Seal() { // 99% case - Style is already sealed. if (_sealed) { return; } // Most parameter checking is done as "upstream" as possible, but some // can't be checked until Style is sealed. if (_targetType == null) { throw new InvalidOperationException(string.Format("Must have non-null value for '{0}'.", "TargetType")); } if (_basedOn != null) { if (_basedOn.TargetType == null || !_basedOn.TargetType.IsAssignableFrom(_targetType)) { throw new InvalidOperationException(string.Format("Can only base on a Style with target type that is base type '{0}'.", _targetType.Name)); } } // Seal setters if (_setters != null) { _setters.Seal(); } // Will throw InvalidOperationException if we find a loop of // BasedOn references. (A.BasedOn = B, B.BasedOn = C, C.BasedOn = A) CheckForCircularBasedOnReferences(); // Seal BasedOn Style chain if (_basedOn != null) { _basedOn.Seal(); } // // Build shared tables // // Process all Setters set on the selfStyle. This stores all the property // setters on the current styles into PropertyValues list, so it can be used // by ProcessSelfStyle in the next step. The EventSetters for the current // and all the basedOn styles are merged into the EventHandlersStore on the // current style. ProcessSetters(this); // Process all PropertyValues (all are "Self") in the Style // chain (base added first) EffectiveValues = new Dictionary <DependencyProperty, object>(); ProcessSelfStyles(this); // All done, seal self and call it a day. _sealed = true; }
public void SetterBaseCollection_Clear_When_Sealed() { var sbc = new SetterBaseCollection(); sbc.Seal(); sbc.IsSealed.Should().BeTrue(); Assert.ThrowsException <InvalidOperationException>(() => sbc.Clear()); }
public void SetterBaseCollection_RemoveAt_When_Sealed() { var sbc = new SetterBaseCollection(); sbc.Add(new Setter(Control.IsTabStopProperty, false)); sbc.Seal(); sbc.IsSealed.Should().BeTrue(); Assert.ThrowsException <InvalidOperationException>(() => sbc.RemoveAt(0)); }
public void SetterBaseCollection_Add_When_Sealed() { var sbc = new SetterBaseCollection(); sbc.Seal(); sbc.IsSealed.Should().BeTrue(); var setter = new Setter(Control.PaddingProperty, new Thickness(20d)); Assert.ThrowsException <InvalidOperationException>(() => sbc.Add(setter)); }
public void SetterBaseCollection_Seal() { var sbc = new SetterBaseCollection { new Setter(FrameworkElement.MarginProperty, new Thickness(10d)), new Setter(Control.PaddingProperty, new Thickness(20d)), new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Red)), }; sbc.IsSealed.Should().BeFalse(); sbc.Seal(); sbc.IsSealed.Should().BeTrue(); foreach (var setter in sbc) { setter.IsSealed.Should().BeTrue(); } }