Skip to content

Commit

Permalink
Fix issue where IconElement.Foreground isn’t always respected
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Apr 24, 2020
1 parent 42de2a4 commit 208bdba
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions ModernWpf/Controls/IconElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,29 @@ private protected IconElement()
{
}

#region Foreground

/// <summary>
/// Identifies the Foreground dependency property.
/// </summary>
public static readonly DependencyProperty ForegroundProperty =
TextElement.ForegroundProperty.AddOwner(
typeof(IconElement),
new FrameworkPropertyMetadata(SystemColors.ControlTextBrush,
FrameworkPropertyMetadataOptions.Inherits));
FrameworkPropertyMetadataOptions.Inherits,
OnForegroundPropertyChanged));

private static void OnForegroundPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
((IconElement)sender).OnForegroundPropertyChanged(args);
}

private void OnForegroundPropertyChanged(DependencyPropertyChangedEventArgs args)
{
var baseValueSource = DependencyPropertyHelper.GetValueSource(this, args.Property).BaseValueSource;
_isForegroundDefaultOrInherited = baseValueSource <= BaseValueSource.Inherited;
UpdateShouldInheritForegroundFromVisualParent();
}

/// <summary>
/// Gets or sets a brush that describes the foreground color.
Expand All @@ -40,6 +55,8 @@ public Brush Foreground
set { SetValue(ForegroundProperty, value); }
}

#endregion

#region VisualParentForeground

private static readonly DependencyProperty VisualParentForegroundProperty =
Expand Down Expand Up @@ -98,6 +115,15 @@ private protected virtual void OnShouldInheritForegroundFromVisualParentChanged(
{
}

private void UpdateShouldInheritForegroundFromVisualParent()
{
ShouldInheritForegroundFromVisualParent =
_isForegroundDefaultOrInherited &&
Parent != null &&
VisualParent != null &&
Parent != VisualParent;
}

private protected UIElementCollection Children
{
get
Expand Down Expand Up @@ -141,8 +167,7 @@ protected override Size ArrangeOverride(Size finalSize)
protected override void OnVisualParentChanged(DependencyObject oldParent)
{
base.OnVisualParentChanged(oldParent);

ShouldInheritForegroundFromVisualParent = Parent != null && VisualParent != null && Parent != VisualParent;
UpdateShouldInheritForegroundFromVisualParent();
}

private void EnsureLayoutRoot()
Expand All @@ -161,6 +186,7 @@ private void EnsureLayoutRoot()
}

private Grid _layoutRoot;
private bool _isForegroundDefaultOrInherited = true;
private bool _shouldInheritForegroundFromVisualParent;
}
}

0 comments on commit 208bdba

Please sign in to comment.