I am in a similar situation, whereby I need to style a datagrid cell based on it's contents, either altering the foreground/background based on the value of a 'Decimal' field, setting the justification for date fields and making the date display as a short
date and setting colour and justification on text fields.
The datagrid is bound to a data table and is dynamically created from various SQL calls (being a 'common' datagrid).
The contents of e.row are a datarow.rowview and contain the row details as expected.
The problem is, with the above code, the following line always returns a null, as does any attempt to get the cellcontent
FrameworkElement fe = column.GetCellContent(e.Row);
If I apply a 'restyle' via a function from a button click, it works once, but then crashes if I try to perform the restyle again. It also loses the styling if the datagrid is sortged by clicking on the columnheader and any attempt to re-apply the style
fails. Another stange action is, once the grid has been dynamically created, if I then call the function to apply the style, the grid is dislayed *without* the modified style. The button on my form calls the exact same function and does nothing else, but this
*does* apply the style. It almost seems to me that though the grid is built, because it has not been displayed, the style isn't "known".
If there is a way code wise, or somehow via a style on the datagridcell which could react to the column datatype to call an approriate converter, I'd be very grateful for some pointers as countless hours searching the web has not revealed any answers.
I am presuming this is actually do-able of course as unfortunately most of the example I find relating to converters and styling assume the datagrid has the columns hard-set in the XAML. Given that a grid where the explicit column is affected by a converter
retains it's styling when the grid is resorted on different columns, I am hoping it is possible.
Terian
Member
4 Points
2 Posts
Re: Re: Change the style of cell within datagrid
Feb 12, 2009 01:22 PM | LINK
Hello,
I am in a similar situation, whereby I need to style a datagrid cell based on it's contents, either altering the foreground/background based on the value of a 'Decimal' field, setting the justification for date fields and making the date display as a short date and setting colour and justification on text fields.
The datagrid is bound to a data table and is dynamically created from various SQL calls (being a 'common' datagrid).
The contents of e.row are a datarow.rowview and contain the row details as expected.
The problem is, with the above code, the following line always returns a null, as does any attempt to get the cellcontent
FrameworkElement fe = column.GetCellContent(e.Row);
If I apply a 'restyle' via a function from a button click, it works once, but then crashes if I try to perform the restyle again. It also loses the styling if the datagrid is sortged by clicking on the columnheader and any attempt to re-apply the style fails. Another stange action is, once the grid has been dynamically created, if I then call the function to apply the style, the grid is dislayed *without* the modified style. The button on my form calls the exact same function and does nothing else, but this *does* apply the style. It almost seems to me that though the grid is built, because it has not been displayed, the style isn't "known".
If there is a way code wise, or somehow via a style on the datagridcell which could react to the column datatype to call an approriate converter, I'd be very grateful for some pointers as countless hours searching the web has not revealed any answers.
I am presuming this is actually do-able of course as unfortunately most of the example I find relating to converters and styling assume the datagrid has the columns hard-set in the XAML. Given that a grid where the explicit column is affected by a converter retains it's styling when the grid is resorted on different columns, I am hoping it is possible.