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?
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 yourTextBlock
'sFontWeight
property to theSameAsLast
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 theTextBlock
to"{Binding}"
, and with that use a anIValueConverter
that takes the item and checks for its equality with the previous item. You'll want to pass the entire collection as theConverterParameter
and - making sure the collection is anIList<T>
- useIndexOf
to get the index of the item in question, useElementAt
to find the previous one, and then check for equality, returning the appropriateFontWeight
.Of course there are no doubt a multitude of other ways this can be done but hopefully you get the idea.