C# – Best way to set a strikethrough on individual cells of the WPF DataGrid

cdatagridstrikethroughwpfxaml

What is the best (easy) way to set the font to a strikethrough style on individual cells of the WPF DataGrid?

Options that I'm aware of are inserting TextBlock controls in individual cells or using a DataGridTemplateColumn – and using the TextDecorations property therein. Either way this is quite a mission, I'd like to use the default AutoGenerate Columns function of DataGrid, especially since my ItemsSource is a DataTable.

As and aside, is there any way to access the TextBlock generated using the default DataGridTextColumn?

Best Answer

<DataGridTextColumn Binding="{Binding Name}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="TextDecorations" Value="Strikethrough"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

Of course you can wrap the setter in a DataTrigger to use it selectively.

Related Topic