Search code examples
c#wpfxamlstyling

Is there a way to specify a textblock style which takes a given font color/brush and reduces its saturation


I would like to keep a predefined style from a theme which defines the color of a textblock, but modify this color by just decreasing its saturation.

Basically if the original color was black, it want the new color to be dark grey.

This has to be done dynamically, as the original color of the text will vary and is not fixed to black.

My best guess was to use a style which uses the original ForeGround property of the Textblock and runs it through a converter.

<Style x:Key="InlineDescriptionStyle" TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
        <Setter Property="FontSize" Value="10"/>
        <Setter Property="Foreground" Value="{TemplateBinding Foreground, Converter=SaturationReducer}"/>
</Style>

However this yields the following error:

Error MC3029: 'Foreground' member is not valid because it does not have a qualifying type name. (12, 39)

Solution

  • Ok, after playing around with the shaders I came up with a solution which is so simple it is almost sad:

    By using the Opacity property of the TextBlock/FrameworkElement I was able to get the desired effect.

    <Style x:Key="InlineDescriptionStyle" TargetType="{x:Type FrameworkElement}">
        <Setter Property="TextBlock.FontSize" Value="10"/>
        <Setter Property="Opacity" Value="0.5"/>
    </Style>
    

    Here an image of the final result: enter image description here