Setting properties directly to GridCell

I am trying to set some properties directly to the grid cells. (Background, Foreground etc..)

The way I do is to have a custom row generator, by overriding the GetGridCell Method, then I am able to access the gridcell reference.

After caching those gridcells, I got a timer to change the properties. 
It works fine, I did see the properties are being updated, but it will also remove some datagrid default behaviours after that. (like selection mode, foreground (it will become black once I selected that cell)), I assume it falls back to some default settings.

I attached a simple demo project to show two datagrids(with exact same settings). And one of them is having timer to change the grid cells' properties. Then that one will lost some behaviours that I set to the datagrid.

I am not looking for the solution to styling the cell.
I am trying to find out if there is a way to keep those settings in datagrid and also setting the properties to the gridcell.


Attachment: DatagridTest_7c69c35d.zip

11 Replies 1 reply marked as answer

SS Susmitha Sundar Syncfusion Team August 24, 2020 06:08 PM UTC

Hi Will Liu, 
 
Thank you for using Syncfusion controls. 
 
Row/Cell selection color is applied behind the GridCell content. So when you apply the background for GridCell, the selection is not displayed in UI. You can resolve this by set the opacity for GridCell background color. 
 
 
internal void ChangeFormat() 
{ 
    foreach (var cell in gridCells) 
    { 
        var i = r.Next(0, 10); 
 
        if (i < 2) 
        { 
            cell.Background = new SolidColorBrush(Colors.Yellow) { Opacity = 0.5 }; 
            cell.Foreground = Brushes.Brown; 
            cell.FontWeight = FontWeights.UltraBold; 
            cell.BorderBrush = Brushes.Red; 
            cell.BorderThickness = new Thickness(2); 
        } 
        else if (i < 5) 
        { 
            cell.Background = new SolidColorBrush(Colors.Blue) { Opacity = 0.5 }; 
            cell.Foreground = Brushes.White; 
            cell.FontWeight = FontWeights.SemiBold; 
            cell.BorderBrush = Brushes.AntiqueWhite; 
            cell.BorderThickness = new Thickness(1); 
        } 
        else if (i < 8) 
        { 
            cell.Background = new SolidColorBrush(Colors.White) { Opacity = 0.5 }; 
            cell.Foreground = Brushes.Blue; 
            cell.FontWeight = FontWeights.Normal; 
            cell.BorderBrush = Brushes.Gray; 
            cell.BorderThickness = new Thickness(1); 
        } 
        else 
        { 
            cell.Background = new SolidColorBrush(Colors.Brown) { Opacity = 0.5 }; 
            cell.Foreground = Brushes.Yellow; 
            cell.FontWeight = FontWeights.Normal; 
            cell.BorderBrush = Brushes.Black; 
            cell.BorderThickness = new Thickness(2); 
        } 
    } 
}  
 
Row hovering: 
 
 
Extended selection: 
 
 
 
Please check the sample and let us know if you need further assistance on this. 
 
Regards, 
Susmitha S 



WL Will Liu August 25, 2020 12:39 AM UTC

That is what I looking for.
However I got one more problem which is the foreground color become black after I selected that cell.
Looks like it got reverted to some default setting.

Actuall behavior:
1. foreground set to blue
2. select that cell and select the some other cell
3. the first cell's foreground become black

What I want is it will still the same color even after selection.


MA Mohanram Anbukkarasu Syncfusion Team August 25, 2020 02:22 PM UTC

Hi Will, 

Thanks for the update.  

We have checked the reported issue and we could see that it occurs only when the SelectionUnit is Cell. We are currently validating this. We will update with further details on 27th August 2020. We appreciate your patience  until then.  

Regards, 
Mohanram A. 



MA Mohanram Anbukkarasu Syncfusion Team August 27, 2020 02:13 PM UTC

Hi Will,  

We regret for the inconvenience. 

We are still validating this issue. As it occurs only when the SelectionUnit is Cell, we are unable to find the exact cause for this issue. We are in need of two more business days to validate this properly. We will update with further details on 31st August 2020. We appreciate your patience  until then.   

Regards,  
Mohanram A. 



WL Will Liu August 28, 2020 07:01 AM UTC

While waiting for that issue, I would like to come back to the first question I have.

I just noticed that if I apply { Opacity = 0.5 } to the background color, then the color I set is different than what I want, is there a way to avoid this?

For exmaple:
var brush1 = new SolidColorBrush(Colors.Blue)
var brush2 = new SolidColorBrush(Colors.Blue) { Opacity = 0.5 } 
var brush3 = new SolidColorBrush(Colors.Blue) { Opacity = 0.8 } 

All of them will show different colors in the grid, what I want do is to keep color I set (in this case will be brush1) then also keep the selection color.


MA Mohanram Anbukkarasu Syncfusion Team August 31, 2020 01:21 PM UTC

Hi Will, 

Thanks for the update.  

Query 
Response 
I just noticed that if I apply { Opacity = 0.5 } to the background color, then the color I set is different than what I want, is there a way to avoid this? 
There is no other way to avoid this behavior. Based on our control structure, the background color applied for the cell will be above the selection/hover color. Hence the only way to resolve this is to set opacity for the Background brush. 
Actuall behavior: 
1. foreground set to blue 
2. select that cell and select the some other cell 
3. the first cell's foreground become black 

What I want is it will still the same color even after selection. 
We have checked this. This will occur only using CustomerRowGenerator to change the foreground color of the cells. We suggest you to achieve your styling related requirement by including Style with terget type as GridCell as given in the following references. 

UG references :  



Please let us know if you have any concerns in this.  

Regards, 
Mohanram A. 



WL Will Liu September 1, 2020 08:07 AM UTC

I already read through that page, I want to do the cell level binding instead of column level binding with converter or data triggers.
It seems that I can only set the cell style for entire datagrid, which is not useful.

Refer to the second question.
I digged into the source code, I found out in GridCell.OnSelectionBorderVisiblityChanged, after the selection border Visibility changed to not Visible it will clear the Foreground value which clear the binding as well.

Below is the source code
        private static void OnSelectionBorderVisiblityChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            var gridCell = obj as GridCell;

            // If SelectionBorderVisiblity is Visible, bind GridCell Foreground with SelectionForegroundBrush
            if (args.NewValue.Equals(Visibility.Visible))
            {
                var dataGrid = gridCell.ColumnBase.Renderer.DataGrid;
#if WPF
                if (gridCell.SelectionForegroundBrush != SfDataGrid.DEFAULT_SELECTIONFOREGROUND)
                    gridCell.Foreground = gridCell.SelectionForegroundBrush;
                else if (SfDataGrid.DEFAULT_SELECTIONFOREGROUND != gridCell.ColumnBase.Renderer.DataGrid.SelectionForegroundBrush)
                    gridCell.Foreground = dataGrid.SelectionForegroundBrush;
#else
                if (!dataGrid.IsEqualBrush(gridCell.SelectionForegroundBrush, SfDataGrid.DEFAULT_SELECTIONFOREGROUND))
                    gridCell.Foreground = gridCell.SelectionForegroundBrush;
                else if (!dataGrid.IsEqualBrush(SfDataGrid.DEFAULT_SELECTIONFOREGROUND, gridCell.ColumnBase.Renderer.DataGrid.SelectionForegroundBrush))
                    gridCell.Foreground = dataGrid.SelectionForegroundBrush;
                else if (ForegroundProperty.GetMetadata(typeof(FrameworkElement)).DefaultValue == gridCell.Foreground)
                    gridCell.Foreground = SfDataGrid.DEFAULT_SELECTIONFOREGROUND;
#endif
            }
            else
                gridCell.ClearValue(GridCell.ForegroundProperty);
        }


MA Mohanram Anbukkarasu Syncfusion Team September 1, 2020 02:42 PM UTC

Hi Will, 

Thanks for the update 

We are currently validating this in our end to find the feasibility to resolve this. We will update with further details on 3rd September 2020. We appreciate your patience until then. 

Regards, 
Mohanram A. 



VS Vijayarasan Sivanandham Syncfusion Team September 3, 2020 01:58 PM UTC

Hi Will Lu,

Thank you for your patience.

You are setting the GridCell style at custom row generator in application. Based on SfDataGrid architecture this not proper way to define the Grid Cell Style in Custom Row generator. So, we have provided the support for applying GridCell Style in SfDataGrid by proper way in below mentioned user documentation for your reference,

UG Link: https://help.syncfusion.com/wpf/datagrid/conditional-styling#conditional-styling-of-cells-using-converter 

https://help.syncfusion.com/wpf/datagrid/conditional-styling?cs-save-lang=1&cs-lang=xaml#condition-styling-of-cells-based-on-record-using-converter  

https://help.syncfusion.com/wpf/datagrid/conditional-styling?cs-save-lang=1&cs-lang=xaml#conditional-styling-of-cells-using-triggers 

https://help.syncfusion.com/wpf/datagrid/conditional-styling?cs-save-lang=1&cs-lang=xaml#conditional-styling-of-cells-using-style-selector  
Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S


Marked as answer

WL Will Liu September 16, 2020 02:17 AM UTC

Thanks for your reply.

I already read through the documents, those solutions are not ideal for my situation.

I need to have a dynamic cell style which can be changed accroding to the data (Cell level).

By using Converter approache I basically have to dupplicate the logic for each properties (each property binding to one converter)
By using Data Trigger approache, setters are well defined at the begining, I cannot change that afterward.

I already posted a question before. 
Fyi https://www.syncfusion.com/forums/156655/sfdatagrid-add-cell-style-binding-to-each-gridcell

Then I found a way to do the driect binding, that seems to work perfectly in my case. But it have some issues on post-selection color and opacity.
So my question is 
1. Is there any other way I can do to achieve cell level binding
2. Is it possible to fix the issues I posted before (post-selection color and opacity)




MA Mohanram Anbukkarasu Syncfusion Team September 16, 2020 03:11 PM UTC

Hi Will, 

Thanks for the update.  

As we mentioned in our previous updates, those are the ways to apply style for the cells in the DataGrid and it is not possible to change the opacity and selection behavior for the cells. We regret for the inconvenience and appreciate your understanding. Please let us know if you require any other assistance from us.  

Regards, 
Mohanram A. 


Loader.
Up arrow icon