CBreeze
CBreeze

Reputation: 2965

Style Only One Column's Cells DataGrid C# WPF

I currently style a DataGridRow based on the value of a column. The style looks like this;

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Overdue}" Value="1">
                <Setter Property="FontWeight" Value="Bold"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Priority}" Value="High">
                <Setter Property="Background" Value="Red"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Priority}" Value="Medium">
                <Setter Property="Background" Value="Orange"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Priority}" Value="Low">
                <Setter Property="Background" Value="LightGreen"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

This applies the style to the entire row based on the column's values. Now however I would to apply the style to only the columns in that cell. This is what the style currently looks like;

enter image description here

However I would like to remove the styling in the cells with a line through below;

enter image description here

i.e. only apply the formatting to the priority column's cells rather than the whole row.

Upvotes: 1

Views: 1594

Answers (1)

itzmebibin
itzmebibin

Reputation: 9439

You can try CellStyle for DataGridTemplateColumns. Ref.

Like, Add the following style "DGCellStyle" in the resource.

    <Style x:Key="DGCellStyle" TargetType="DataGridCell">
            <Style.Triggers>
            <DataTrigger Binding="{Binding Overdue}" Value="1">
                <Setter Property="FontWeight" Value="Bold"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Priority}" Value="High">
                <Setter Property="Background" Value="Red"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Priority}" Value="Medium">
                <Setter Property="Background" Value="Orange"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Priority}" Value="Low">
                <Setter Property="Background" Value="LightGreen"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

Then, refer this style in DataGridTemplateColumn.

<DataGridTemplateColumn Header="ColumnHeader" Width="SizeToHeader" CellStyle="{StaticResource DGCellStyle}"/>

Upvotes: 4

Related Questions