Click here to Skip to main content
14,830,805 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I have a DataGrid with some columns. I need to color part of the columns based on their value. Thus I have made an IValueConverter implementation. I added a background property setter to the style:
XML
<Style x:Key="CenterCellStyle" TargetType="{x:Type DataGridCell}">
  <Setter Property="Background" Value="{Binding ???, Converter={StaticResource ResourceKey=AlarmConverter}}" />
...

The affected cells look like this:
XML
<DataGridTextColumn Header="Black" Binding="{Binding [1]}" Width="100" CellStyle="{StaticResource CenterCellStyle}"/>

If I write in place of ??? any property name of the class bound to the grid, all affected cells in the row will be colored based on that property. But I need to color every cell based on it's own value.

What should I write in place of ??? ?

I have found here[^] the following expression that looked promising: {Binding Text, RelativeSource={RelativeSource Self}}, but it is not working - I suppose because DataGridTextColumn has no Text property, and I should bind to the field bound to the cell, but with some relative reference. But don't know how.

Please advice. Thank you.
Posted

I have solved it myself:
XML
<setter property="Background" value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource ResourceKey=AlarmConverter}}" />

... and the object passed to the converter is the string in the cell. And that is what I needed.

[Why can't I delete my solution - and can't I add my own solution properly right from the start, not only after an other is posted?]
   
v3
XML
<setter property="Background" value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource ResourceKey=AlarmConverter}}" />
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900