Evaluate Datagrid row value with previous value

237 Views Asked by At

I need to create a new property for DataGrid in DataGridTemplateColumn, where this property will be boolean and will indicate whether the column will be evaluated or not by the following rule; When the value of the current row is different from the previous row, the cell should be bold.

<DataGridTextColumn Header = "SG"
                    Binding="{Binding SteelGrade}"
                    IsEvaluated="True" <!-- indicates that this column will be bold if the current value is different from the previous line value-->
>
</ DataGridTextColumn>

So I need to create IsEvaluated and also the rule.

Would anyone have any ideas or a link that could show me how I could do this?

1

There are 1 best solutions below

0
On

This shouldn't be too hard. There are a couple of different ways you could do this. One way would be to make sure every item in your collection has a reference to the previous item (make sure it's a WeakReference to avoid garbage collection issues!) Then make a property for your item, SameAsLast, which simply checks for equality with the previous item. Finally, bind your TextBlock's FontWeight property to the SameAsLast with an appropriate converter. This is probably the most efficient option, but it does require building the chain of references every time the items are sorted.

If you are looking for a generic way of doing this without modifying the item class itself, this is also possible. You could, for example, set the FontWeight property of the TextBlock to "{Binding}", and with that use a an IValueConverter that takes the item and checks for its equality with the previous item. You'll want to pass the entire collection as the ConverterParameter and - making sure the collection is an IList<T> - use IndexOf to get the index of the item in question, use ElementAt to find the previous one, and then check for equality, returning the appropriate FontWeight.

Of course there are no doubt a multitude of other ways this can be done but hopefully you get the idea.