Strikethrough on celltext

Sep 12, 2009 at 11:49 AM

Hi!

First of all, I'm fairly new to WPF, so I'm in a learning progress, and not familiar with everything yet, so this may be an easy question to answer!

SO, to the question!

I have some rows I need to have a strikethrough text on, but can't figure out how to set the style. My columns is DataGridTextColumn, so it contains a TextBlock which has the ability to have a TextDecoration, but I can't figure out how.

I have search a bit around, but didn't find any useful information to do this. Since I'm new to WPF I can't tell if this is a DataGrid question or a general WPF question.

Thanks,
--Rune

Apr 18, 2012 at 9:02 PM
Edited Apr 18, 2012 at 9:10 PM

I know it is about 3 years too late but... Here is a render expensive way to do what you are interested in doing.

1. Define a TextBox style for the DataGridTextColumn's text box, or any other TextBox whose DataContext contains the bound property...

    <Style x:Key="DataGridTextColumnTextBoxStyle" TargetType="{x:Type TextBlock}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding SomeBool}" Value="True">
                <Setter Property="TextDecorations">
                    <Setter.Value>
                        <TextDecorationCollection>
                            <TextDecoration Location="Strikethrough" >
                                <TextDecoration.Pen>
                                    <Pen Thickness="2" Brush="{DynamicResource ItronBlackSolidColorBrush}"/>
                                </TextDecoration.Pen>
                            </TextDecoration>
                        </TextDecorationCollection>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding SomeBool}" Value="False">
                <Setter Property="TextDecorations" Value="None"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

2. The apply it to your TextBox, or in this case the ElementStyle for the DataGridTextColumn

                <DataGridTextColumn Header="{x:Static Properties:Resources.ColumnHeader}" 
Binding
="{Binding BoundProperty}" 
ElementStyle
="{DynamicResource DataGridTextColumnTextBoxStyle}" />

Rending seems to really slow down since each TextBox has to evaluate the bound property, in this case "SomeBool". It is possible but very fast...