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
WeakReferenceto avoid garbage collection issues!) Then make a property for your item,SameAsLast, which simply checks for equality with the previous item. Finally, bind yourTextBlock'sFontWeightproperty to theSameAsLastwith 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
FontWeightproperty of theTextBlockto"{Binding}", and with that use a anIValueConverterthat takes the item and checks for its equality with the previous item. You'll want to pass the entire collection as theConverterParameterand - making sure the collection is anIList<T>- useIndexOfto get the index of the item in question, useElementAtto 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.