/// <summary> /// Apply the default type style and the style-Property /// </summary> protected void ApplyStyle() { // gets the default type style, like this <Style x:key="{x:Type Button}"... /> var defaultStyle = Style.GetStyleResource(this.GetType()); // check whether a default type style exists and if exists // the overridesdefaultstyle-property is false // apply default style, if style isn't set or OverridesDefaultStyle-property is false if (defaultStyle != null && (this.Style == null || !this.Style.OverridesDefaultStyle)) { this.ApplyStyle(defaultStyle); } if (this.Style != null && defaultStyle != this.Style) { // apply style-property this.ApplyStyle(this.Style); } this.Invalidate(); }
/// <summary> /// check whether a trigger has been fired /// </summary> /// <param name="propertyName"></param> void CheckTrigger(DependencyProperty dp) { var propTriggers = new List <Trigger> (); if (this.Style != null) { foreach (var trig in this.Style.Triggers) { if (trig.Property == dp) { propTriggers.Add(trig); } } } if (this.Style == null || !this.Style.OverridesDefaultStyle) { // gets the default style based on the current type var defaultStyle = Style.GetStyleResource(this.GetType()); if (defaultStyle != null) { // iterate triggers foreach (var trig in defaultStyle.Triggers) { var count = propTriggers .Where(p => p.Property.Name == trig.Property.Name) .Count(); // check whether the trigger-property does not overwrite the default style if (count == 0) { if (trig.Property == dp) { propTriggers.Add(trig); } } } } } if (propTriggers.Count > 0) { // get current property value var propValue = this.GetCurrentValue(dp.Name); foreach (var trig in propTriggers) { // check whether the trigger is active var active = (trig.Value == null && propValue == null) || (trig.Value != null && trig.Value.Equals(propValue)); if (active) { // apply trigger this.ApplyTrigger(trig); // set trigger as active this.SetTriggerState(trig, true); } else { // check whether a trigger has already been activated if (this.IsActiveTrigger(trig)) { // set trigger as disabled this.SetTriggerState(trig, false); // reset changed trigger-values this.ResetTrigger(trig); // reset stored values this.ResetStoredValues(trig); } } } } }