When data is bound to a grid, how can I change the color of a label, programmatically, when a value is negative?
Hello PMac,
Allow me to start by saying that I would highly recommend against setting the colors of the cell presenters and record presenters programmatically, as you will almost certainly run into issues with the virtualization of the XamDataGrid. With that said, you can still set it by using the CellValuePresenter.FromCell method on the cell that owns the value that has changed, but again, if the virtualization of the grid is on, you will run into odd styling issues with this because the CellValuePresenter elements are recycled and reused.
Instead, I would recommend that you write a Style for CellValuePresenter to handle this. The data context of the CellValuePresenter is the underlying DataRecord to which it belongs, which has a DataItem property to return your underlying data item. So, if you bind a DataTrigger to DataItem.<propertyName>, you can then use a converter to check if your value is null and return a value to the DataTrigger that designates this. Then, in the DataTrigger, you can set the Foreground-related properties of your CellValuePresenter to the desired "negative-value" color.
Note, if you leave this Style implicit, it will affect each and every cell in your grid and so the cells for an entire row will have that foreground. If you only wish to have it effect certain Fields, I would recommend giving your Style an x:Key and applying it to your Fields' CellValuePresenterStyle properties using a StaticResource binding.
I have attached a sample project to demonstrate the above. I hope this helps you.
Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate Developer
Thank you for your help. I am using Autotgenerated="False", hence why I was thinking of doing this programmatically.
I'll attempt the Style approach.... although I'm on V14.1 and do not see CellValuePresenterStyle attribute available for Field tags.
The sample project is also using XamDataGrid.FieldLayoutSettings.AutoGenerateFields = "False." I'm unsure why this necessitates setting your colors programmatically, though?
You are correct that the CellValuePresenterStyle property did not exist directly on the Field in 14.1. In that version, this property existed on the FieldSettings element which can be used for the Settings property of each Field. You can set the CellValuePresenterStyle property in XAML like so in 14.1:
<ig:Field><ig:Field.Settings><ig:FieldSettings CellValuePresenterStyle="{StaticResource style}" /></ig:Field.Settings></ig:Field>
Sorry, type-o.... I meant to say where AutoGenerateFields = "True"... which is the case I am testing
In the case that you are Auto-Generating the Fields in the XamDataGrid, I would recommend that you obtain the desired Field and place your CellValuePresenter style on that Field's FieldSettings.CellValuePresenterStyle property programmatically.
You can do this by giving the Style for CellValuePresenter an x:Key. Then, you can get it by using the following code, where "owningElement" is the <Element>.Resources that you have placed the style in:
Style style = owningElement.Resources["myKey"] as Style;
Once you obtain this, you can utilize either the Loaded or FieldLayoutInitialized event of the XamDataGrid to get the Field that you are looking to set the CellValuePresenter style on, and set it. You can use the following sample code for this, where "xdg1" is the XamDataGrid:
//In the Loaded event for the XamDataGrid...Field field = xdg1.FieldLayouts[0].Fields["myField"];field.Settings.CellValuePresenterStyle = style;