Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sadly the way the styles work in WPF you can't create a generic style on a parent class and have it apply to the sub-classed control.</p> <p>One thing you can do is create a base style that targets a base type with the property you want to set (like ContentControl) and then create a specific style for each control that is BasedOn that style. Here is an example:</p> <pre><code>&lt;Window&gt; &lt;Window.Resources&gt; &lt;Style x:Key="BaseContentControlStyle" TargetType="{x:Type ContentControl}"&gt; &lt;Setter Property="Foreground" Value="Red" /&gt; &lt;/Style&gt; &lt;Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseContentControlStyle}" /&gt; &lt;Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseContentControlStyle}" /&gt; &lt;/Window.Resources&gt; &lt;StackPanel&gt; &lt;Label Content="Test"/&gt; &lt;Label Content="Test"/&gt; &lt;CheckBox Content="Checkbox"/&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>Hope this helps.</p> <p>EDIT:</p> <p>You could use Pavlo's method below for restoring inheritance and make it a little easier to use like so:</p> <pre><code>&lt;Window.Resources&gt; &lt;Style x:Key="BaseContentControlStyle" TargetType="{x:Type 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 BaseContentControlStyle}" /&gt; &lt;Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseContentControlStyle}" /&gt; &lt;/Window.Resources&gt; </code></pre> <p>At least then you don't have to copy the same setter code everywhere (BTW, I think the TextBlock inherits by default; no default style with overrides).</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