Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is because such controls as <code>Label</code> and <code>CheckBox</code> override the <code>Foreground</code> property in their styles. </p> <p>Below is an example a typical logical tree of elements that shows how the value specified on the <code>Window</code> level travels down the tree:</p> <pre><code>Window (Red [Local]) -&gt; Grid (Red [Inherited]) -&gt; ListBox (Red [Inherited]) -&gt; ListBoxItem (Red [Inherited]) -&gt; StackPanel (Red [Inherited]) -&gt; Label (Black [Style]) -&gt; TextBlock (Black [Inherited]) -&gt; TextBlock (Red [Inherited]) </code></pre> <p><em>In square brackets the source of the value is shown.</em></p> <p>As you can see the inheritance breaks on the <code>Label</code> itself because it has the <code>Foreground</code> property set in its default style:</p> <pre><code>&lt;Style x:Key="{x:Type Label}" TargetType="{x:Type Label}"&gt; &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/&gt; ... &lt;/Style&gt; </code></pre> <p>As a workaround for this we can use the following trick. Define the default style for such controls (as <code>Label</code>) in the application (in App.xaml or in the <code>Window</code> inself). And in that default style override the <code>Foreground</code> property to set a relative source binding to the nearest ancestor of the control that still has the desired value: </p> <pre><code>&lt;Style TargetType="{x:Type Label}"&gt; &lt;Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/&gt; &lt;/Style&gt; &lt;Style TargetType="{x:Type CheckBox}"&gt; &lt;Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/&gt; &lt;/Style&gt; </code></pre> <p>After that our tree will look like this:</p> <pre><code>Window (Red [Local]) -&gt; Grid (Red [Inherited]) -&gt; ListBox (Red [Inherited]) -&gt; ListBoxItem (Red [Inherited]) -&gt; StackPanel (Red [Inherited]) -&gt; Label (Red [Binding to StackPanel.(TextElement.Foreground)]) -&gt; TextBlock (Red [Inherited]) -&gt; TextBlock (Red [Inherited]) </code></pre> <p>As you can see, our binding restores the inheritance.</p> <p>Such styles need to be defined for each element that overrides the <code>Foreground</code> property in its style. As @Duane suggested, to not duplicate the binding in each style the <code>BasedOn</code> capability can be used:</p> <pre><code>&lt;Style x:Key="ForegroundInheritanceFixStyle" TargetType="Control"&gt; &lt;Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/&gt; &lt;/Style&gt; &lt;Style TargetType="{x:Type Label}" BasedOn="{StaticResource ForegroundInheritanceFixStyle}"&gt; &lt;/Style&gt; &lt;Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource ForegroundInheritanceFixStyle}"&gt; &lt;/Style&gt; </code></pre> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload