I'm trying to set a static style to the property "CellValuePresenterStyle" of a field, e.g. with some particular background color. However, the field does not get refreshed to reflect the new style.
----------------------------------
xamDataGrid1.FieldLayouts[0].Fields[0].Settings.CellValuePresenterStyle = (
Style)style;
------------------------------
In the previous 9.1 version , we had a work-around to this problem. i. e. in code, we collapse a record then make it visiable again.
But it does not work anymore in 9.2 version!
Attaching a simple project to reproduce the situation. In the project, supposingly if you click on any cell of the grid, the background color of the first column should become light green.
With 9.1 version it works with the work-around I mentioned.
With 9.2 version it never works. (Feel free to change the reference lib yourself to try out).
Thanks for the help!
Hello,
In 9.1. and 9.2 versions there is no direct way to set the CellValuePresenter style without refreshing the layout. Yes there are couple of workarounds to do this like showing/hiding the fields in 9.1 or grouping/ungrouping in 9.2 but we do not recommend niether of this approaches. You can achieve the same behaviour with a style for the CellValuePresenter and converter for the background color.
if (e.Type == typeof(Cell)) { object style = xamDataGrid1.TryFindResource("LightGreenBackgroundStyle"); xamDataGrid1.FieldLayouts[0].Fields[0].Settings.CellValuePresenterStyle = (Style)style;
xamDataGrid1.FieldLayouts[0].SortedFields.Add(new FieldSortDescription() { FieldName = "name", IsGroupBy=true }); xamDataGrid1.FieldLayouts[0].SortedFields.RemoveAt(0);
}
Is there any reasons to change the default CellValuePresenter's style in runtime?
There is another approach that you can use in this scenario. You do not need to apply a style and then clear it, but rather take advantage of the WPF Framework's binding engine power. You would also not have to group/ungroup the fields to reinitialize the CellValuePresenter.
You can use an attached property which you can bind to. The attached property will point to the field that you wish to highlight.
I am attaching a small project that demonstrates this issue.
Hope this helps.
Thanks for the reply.
I tried to bind the cell background color with the tag of the DataRecord and it works.
PS: But when i tried to bind to the tag of the Cell, it encountered the same problem (no refreshing until hide/show the column).
And is there any reason why setting the CellValuePresenter directly does not work? Is it a bug? Since this property is not read-only, it makes full sense that the user will expect the style to be effective immediately after the set action.
Thanks.
The CellValuePresenter are virtualized when they are scrolled out of view, so this could be the reasons for this. If you are saving some information in the Tag of the CVP, it is lost. The cell's tag however should be working. Do you have any binding exceptions in the output window? If not, can you provide a small sample with this behavior?