In my xamDataGrid, I set the background color of some cells to gray based on user input - this is working fine. With a reset button above the grid, I want to reset cell backgrounds back to white - this is not working. Here is my code:
foreach (DataRecord dr in xgrdPositions.Records) { foreach (Cell cell in dr.Cells) { cell.DataPresenter.Background = Brushes.White; } }
The cells appear to randomly become gray/white when I click the reset button. Please help - what am I missing? I just need to set the background to white in code behind.
Thanks.
Hello Jay6447,
Thank you for your update, and I'm glad that my sample project and explanation helps you on this matter.
The CellValuePresenter element wraps the editors for the Fields in the XamDataGrid, placing the editors at a lower level. While an editor may not be in "edit mode" when the field is not being edited, the editor is still present, and a background color for that editor will show over the background for a CellValuePresenter.
Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate Developer
Thanks Andrew, your suggestion worked perfectly. Also, thanks for the detailed explanation and the sample.
Yes, you were correct about my second example - I was also styling the editor for the field in XAML and setting the background there. I thought the two would not conflict.
I find it rather strange that your code snippet from your second response to this forum thread is not working for you, as setting a Style for CellValuePresenter which sets the background of the cell should work in this case. I have to be curious as to whether or not you are perhaps styling the editor of your Field and setting the background there? I ask, as the editor is at a lower scope, and as such, if you set the background of it, the background of the cell may not show through.
Regarding your first code-snippet on this matter, this will not set the background on a cellular level. The cell.DataPresenter call will return the XamDataGrid that the cells belong to, and so you are essentially setting the background of the XamDataGrid, and not the CellValuePresenter in this case. If you wish to get the CellValuePresenter from the cell, you could do something along the lines of the following:
CellValuePresenter cvp = CellValuePresenter.FromCell(Cell c);
I would still recommend against doing this programmatically, though, as the cells in the XamDataGrid are virtualized, and their containers are recycled. It is possible to disable this virtualization by setting the CellContainer and RecordContainer GenerationMode properties to "PreLoad," but you will run into performance issues in this case if your data is large, as all of the CellValuePresenters will be kept in memory, and will need to be created at the start of your application.
Instead, to achieve your requirement, I would probably recommend that you define a property on your data item in this case, that you could perhaps use a DataTrigger for and bind to {Binding DataItem.PropertyName} in a CellValuePresenter style. This property could be a bool property that determines whether or not a particular record, or property of a record has been "processed." If this is the case, you could set the Background property of your CellValuePresenter. The issue with this is that if you want this to be specific for every cell, you may need a property for each property on your data item that determines whether or not that property for that data item has been processed, and if you have many Fields, this could mean quite a bit of styling. If you were to do this for the entire data item, every cell in your record would then have this background, as well.
I have attached a sample project to demonstrate the recommendation mentioned above. In this sample, there exists an extra bool property for each property on the underlying data item named <PropertyName>Processed. In a context menu, the SelectedItems.Cells collection of the XamDataGrid is looped through, and reflection is used with each cell's Field.Name + "Processed" to find a property on the underlying data item and set it to true. In the XAML exists a style that will apply to all cells, as each Field has a MultiDataTrigger associated with it checking each CellValuePresenter's Field.Name and the underlying DataItem.<FieldName>Processed property for true.
I hope this helps you. Please let me know if you have any other questions or concerns on this matter.
With further research, it seems that what I am trying to do will not work.
Basically, I want the user to select a range/block of cells, right-click and run some logic on the selected cells with context menu. This process is working fine but I also want to mark these cells (in any way possible) so that the user know that the data in these cells has already been processed. At some point, he should be able to unmark the cells and reset the grid.
I tried to use cell's background property but if there is any other simple way to accomplish the above, please suggest.
THanks.
Note that I have also tried:
Style myStyle = new Style(typeof(CellValuePresenter)); myStyle.Setters.Add(new Setter(CellValuePresenter.BackgroundProperty, Brushes.White)); f.Settings.CellValuePresenterStyle = myStyle; // f is a Field
but, am getting the save behavior.