1

I found this solution here Change DataGrid cell colour based on values but want to take it down another level so that I can have different conditions for changing the color for each cell and assume the most straightforward way would be to apply different styles to each single cell.

In short, what is the XAML to access a singular cell in a datagrid?

UPDATE: At suggestion below (@OmegaMan) found this: How can I use Grid.Row Property as a DataBinding Path for a MultiDataTrigger Condition?

<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=      (Grid.Row)}" Value="2"/> 

but am stuck on how to modify to make it refer to a datagrid column and row kind of like below

    <Style x:Key="colorDataGridCellStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="Background" Value="AntiqueWhite" />
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Property ="Column"
                   Value="2"/>
                    <Condition Property ="Row"
                   Value="2"/>
                    <Condition {the value of this particular cell (2,2) is between two values, thinking this will mostly have to be in the viewModel but maybe can have more here} />
                </MultiDataTrigger.Conditions>
                <Setter Property="Foreground" Value="Green" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>

UPDATE 2 I just found this which seems closer to what I need but I'm not sure how to modify to allow for making it specific to single cells.

How do I change a datagrid row color in XAML based on the value listed in code?

Community
  • 1
  • 1
azulBonnet
  • 831
  • 4
  • 14
  • 31

1 Answers1

1

Use a datatrigger on a style and bind to data which can identify a specific cell, in this case flag anyone whose name is "Johnson":

<Style x:Key="colorDataGridCellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="Background" Value="AntiqueWhite" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Last}" Value="Johnson">
            <Setter Property="Foreground" Value="Blue" />
        </DataTrigger>
    </Style.Triggers>
</Style>

For the target column specify the style

<DataGridTextColumn Header="The Name"
                    Binding="{Binding Last}"
                    CellStyle="{StaticResource colorDataGridCellStyle}"/>

The when the program runs see it in action:

enter image description here

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • How exactly do I "bind to data which can identify a specific cell"? The only binding here is the IsPrimaryCell which just sets off the DataTrigger but it would do it to the whole column no? – azulBonnet May 27 '16 at 16:43
  • @azulBonnet updated example to show specific cell (with a specific data) which triggers a color change. You will need to trigger such a change for a cell with either single trigger as shown or multi-data triggers. See [answer to MultiData Trigger Example](http://stackoverflow.com/a/905940/285795) – ΩmegaMan May 27 '16 at 18:54
  • Thank you but... I still don't understand how to apply the style to a single cell. Within the same column/row I need different triggers. This is still pointing at the entire column. – azulBonnet May 27 '16 at 19:37
  • @azulBonnet before inserting the data create a wrapper which will hold the original data but expose two new properties which will identify a row and column. Then use that info on a multidata trigger to do what you need on a specific cell with the triggers shown above. – ΩmegaMan May 27 '16 at 19:49
  • I'm sorry, really appreciate the help but I still do not follow. :( – azulBonnet May 31 '16 at 17:24
  • @azulBonnet what I am saying is that in XAML a style only knows about a cell type, it doesn't understand the concept of individual cells in specific rows. The only way to give that *omniscience* is to wrap the data into a specific class which exposes the original data's properties but also contains a row # and a cell # (or some other flag to say **highlight me**). Then a style can look for that *Highlight me* property and do its job. – ΩmegaMan Jun 01 '16 at 13:17