wpfstrikethrough

Wpf change color of strikethrough in datatrigger


I have two strikethrough datatrigger, one of them I want to be the color black (default), but one of them should be Strikethrough red, can someone help me with the red color, this is my code:

<TextBlock Name="Number" Grid.Column="0" Text="{Binding Number}"/>
<TextBlock Name="Name" Grid.Column="1" Text="{Binding Name}"/>
<TextBlock Name="Price" Grid.Column="0" Text="{Binding Price}"/>
<TextBlock Name="Stock" Grid.Column="1" Text="{Binding Stock}"/>

<DataTrigger Binding="{Binding IsStrikethroughRed}" Value="True">
    <Setter TargetName="Number" Property="TextBlock.TextDecorations" Value="Strikethrough"/>
    <Setter TargetName="Name" Property="TextBlock.TextDecorations" Value="Strikethrough"/>
    <Setter TargetName="Price" Property="TextBlock.TextDecorations" Value="Strikethrough"/>
    <Setter TargetName="Stock" Property="TextBlock.TextDecorations" Value="Strikethrough"/>    
</DataTrigger>

Solution

  • TextDecorations.Strikethrough is merely a static instance of TextDecorationCollection predefined in TextDecorations class. Likewise, you can define your own TextDecorationCollection and apply it by DataTrigger.

    <Label>
        <Label.Style>
            <Style xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
                   TargetType="{x:Type ContentControl}">
                <Style.Resources>
                    <TextDecorationCollection x:Key="RedStrikethrough"
                                              PresentationOptions:Freeze="True">
                        <TextDecoration Location="Strikethrough">
                            <TextDecoration.Pen>
                                <Pen Brush="Red" Thickness="2"/>
                            </TextDecoration.Pen>
                        </TextDecoration>
                    </TextDecorationCollection>
                </Style.Resources>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ContentControl}">
                            <TextBlock x:Name="Number" Text="{Binding Number}"/>
                            <ControlTemplate.Triggers>
                                <DataTrigger Binding="{Binding IsRemoved}" Value="True">
                                    <Setter TargetName="Number" Property="TextBlock.TextDecorations" Value="{StaticResource RedStrikethrough}"/>
                                </DataTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Label.Style>
    </Label>