How to get RadGridView cell foreground color in WPF

0 Answers 60 Views
GridView
Gennady
Top achievements
Rank 2
Iron
Gennady asked on 12 Aug 2022, 04:51 PM

I have cells highlighted by changing cell' foreground color.

Now i need to collect those highlighted cells to select cells programmatically. I can browse through values but need to read back cell style:

List<GridViewCellInfo> highlightedCells = new List<GridViewCellInfo>();
foreach (var item in this.gridView.Items)
{
     foreach (var column in this.gridView.Columns.OfType<GridViewBoundColumnBase>())
     {
          var cell = column.GetValueForItem(item); // it is actual cell value
// looking for something like: 
// if (cell.Foreground == Brushes.Cyan)
//       highlightedCells.Add(cell)
     }
}
Thank you,
Gennady
Top achievements
Rank 2
Iron
commented on 13 Aug 2022, 01:09 AM

instead of searching for highlighted cell color did switch to another design:

  • created local to xaml.cs private List<GridViewCellInfo> selection;
  • store highlighted cells selection.Add(cell); in MainGrid_SelectedCellsChanged() event
  • before any action will reselect grid with highlighted cells in xaml.cs and in model view will get back selection thisGrid.SelectedCells;

Kind of double selection process: user can click on any grid cells, command will select highlighted cells only. But it works for my project.

 

Martin Ivanov
Telerik team
commented on 16 Aug 2022, 08:05 PM

Thanks for sharing your solution Gennady.

Indeed, working with the GridViewCellInfo is the better approach. This is because the GridViewCell visual elements are not reliable. What I mean is that because of the UI virtualization in the GridView control, these visuals can be replaced by another instances or placed on other positions (related to another data item). 

The better approach here is to work with some kind of data models (like the GridViewCellInfo). An alternative to your approach is to keep the highlight information in the row's data model. This will allow you to assign it to the cell using data binding and then easily collect the information from the ItemsSource of RadGridView.

Gennady
Top achievements
Rank 2
Iron
commented on 17 Aug 2022, 01:50 PM

Thank you, Martin, for your sharing!

Indeed, binding cells to each data in the row is a better approach, straight mvvm workflow. Will redesign in next release.

Best regards,

Gennady

 

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Gennady
Top achievements
Rank 2
Iron
Share this question
or