Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

WPF: Set background color of cells of a particular column based on column index in WPF Datagrid

I want to set the background color of all the cells of a particular column in WPF Datagrid.

The requirement is pretty straight forward and simple but I am pretty new to WPF.

I found hundred of posts like this Style Only One Column's Cells DataGrid C# WPF which are using DataGridCell and DataTrigger combination to set style of a particular cell, but the trigger is always dependent on data on that cell, while I do not want to depend on data but just the column index.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any way to do that?

>Solution :

you can use a simple Style with one setter for Background (individual style for each column):

<DataGridTextColumn Binding="{Binding ...}" Header="Red">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background" Value="Red"/>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding ...}" Header="Green">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background" Value="Green"/>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding ...}" Header="Blue">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background" Value="Blue"/>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading