Search code examples
c#wpftextblocktext-decorations

How to change TextDecoration color in WPF TextBlock?


I am changing the color of the TextDecoration this way:

<Grid Background="{x:Null}"
      Margin="10,0,10,0">
    <TextBlock Text="{Binding Value}"
               VerticalAlignment="Center"
               HorizontalAlignment="Center"
               Style="{StaticResource SWMRegularTextBlockStyle}"
               Margin="0"
               FontSize="{DynamicResource RegularFontSize}"
               x:Name="tb" />
        <Line VerticalAlignment="Center"
              HorizontalAlignment="Center"
              Visibility="{Binding InStock, Converter={StaticResource ReverseBooleanToVisiblity}}"
              Stroke="Red"
              Margin="0"
              StrokeThickness="2"
              X1="1"
              Stretch="Fill"
              Width="{Binding ActualWidth, ElementName=tb, UpdateSourceTrigger=PropertyChanged}" />
</Grid>

But when Text has two lines it fails. Please help me to change the color of TextDecoration. Thanks in advance.

NOTE: I want TextBlock foreground and strike-through line in different colors.


Solution

  • I think this is what you are looking for.

    <TextBlock Text="{Binding Value}" VerticalAlignment="Center" HorizontalAlignment="Center" Style="{StaticResource  SWMRegularTextBlockStyle}" Margin="0" FontSize="{DynamicResource RegularFontSize}" x:Name="tb" >
       <TextBlock.TextDecorations>
            <TextDecoration Location="Strikethrough">
                <TextDecoration.Pen>
                    <Pen Brush="Red" />
                </TextDecoration.Pen>
            </TextDecoration>
        </TextBlock.TextDecorations>
    </TextBlock>