We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to set style on selected cells?

Thread ID:

Created:

Updated:

Platform:

Replies:

144591 May 13,2019 06:28 PM UTC May 29,2019 04:27 AM UTC WPF 6
loading
Tags: SfDataGrid
Carlos Fernando Consigli
Asked On May 13, 2019 06:28 PM UTC

I'm using a sfDataGrid together with a custom StyleSelector because I need to apply some styles according to each cell contents. It works ok but when I select multiple cells, the SelectStyle method in the StyleSelector is not being called. So cells are being actually selected (I can copy them to clipboard and it works) but the selection is invisible, i.e. selected cells don't change its color. 
Even if SelectStyle was called, I don't know which would be the correct way to check if the cell being rendered is selected or not.
When I remove my custom StyleSelector, selected cells correctly get a grey background.
.
How could I achieve that? Thanks in advance.
Greetings,


Adhikesevan Kothandaraman [Syncfusion]
Replied On May 14, 2019 02:15 PM UTC

Hi Carlos, 

Thanks for using Syncfusion products. 

We are currently working on your query with high priority. We will get back to you on this at 16th May, 2019.   
Thanks for your patience until then. 

Regards, 
Adhi 


Dinesh Babu Yadav [Syncfusion]
Replied On May 17, 2019 06:23 AM UTC

Hi Carlos, 
 
Thanks for your patience.  
 
If you want to show the selection of row/cell when setting the background for SfDataGrid’s GridCell, you can change the opacity of the background color brush. Refer to the following KB document, 
 
 
Regards, 
Dinesh Babu Yadav 
 


Carlos Fernando Consigli
Replied On May 25, 2019 02:29 AM UTC

Hi, sorry for the delay answering.
I've already read that documentation, but the problem is that I'm using a custom StyleSelector.
I'm doing something like this programatically:

newDatagrid.CellStyleSelector = new MyCellStyleSelector(newDatagrid);

and that MyCellStyleSelector has business logic that decides the color of the cell and returns the appropriate style.

I tried to do this:
   newDatagrid.RowSelectionBrush = new SolidColorBrush(Colors.Red);

but that only works if I don't assign my custom MyCellStyleSelector.

I'm overriding the SelectStyle method in my custom StyleSelector. I just need a way inside that method to know whether the cell being rendered is selected or not, so I can return the correct "selected" style.

Jagadeesan Pichaimuthu [Syncfusion]
Replied On May 27, 2019 10:18 AM UTC

Hi Carlos, 
  
We analyzed your query to show row/cell selection when using CellStyleSelector. As mentioned in the KB, you need to change the opacity of the background color that you are applying in the cell style selector. On further analysis, we suspect that you are programmatically applying style and that you can programmatically change the opacity of background color as like below code snippet 
  
public class CustomStyleSelector : StyleSelector 
    { 
        public override Style SelectStyle(object item, DependencyObject container) 
        { 
           var businessObject = item as BusinessObjects; 
            Style cellStyle1 = new Style(typeof(GridCell)); 
cellStyle1.Setters.Add(new Setter(GridCell.BackgroundProperty, newSolidColorBrush(Colors.YellowGreen) { Opacity = 0.2 })); 
  
            Style cellStyle2 = new Style(typeof(GridCell)); 
            cellStyle2.Setters.Add(new Setter(GridCell.BackgroundProperty, newSolidColorBrush(Colors.CornflowerBlue) { Opacity = 0.2 })); 
  
            if (businessObject != null) 
            { 
                if (businessObject.EmployeeGender == "Male") 
                    return cellStyle1; 
                else 
                    return cellStyle2; 
            } 
            return base.SelectStyle(item, container); 
        } 
    } 
  
Further, you can check whether or not the item is selected using the code snippet below 
  
var businessObject = item as BusinessObjects; 
bool isSelected = dataGrid.SelectedItems.Contains(businessObject); 
  
Also please refer to the below KB articles to know more details on styling. 
KB Articles: 
  
If the above solution does not meet your requirement, please revert us with code files related to your CustomStyleSelector. If possible, please also revert us with modified sample. It will help us to investigate further and provide earlier prompt solution. 
  
Regards, 
Jagadeesan

Carlos Fernando Consigli
Replied On May 28, 2019 06:15 AM UTC

Thanks for your answer. I will dig my code a little more and try to isolate my issue, maybe I'm messing it up somewhere because  pivotGrid.SelectedItems is always empty, even in the datagrid_SelectionChanged handler. However if I hit "ctrl+C", contents of all "invisibly" selected cells get copied to the clipboard and I can paste them in Excel. So they are actually selected somewhere. Still, both inside datagrid_SelectionChanged handler and in the SelectStyle method, I see always 0 elements in pivotGrid.SelectedItems.
I will try to make a simple sample where this is reproducible and upload it tomorrow.
Greetings

Dinesh Babu Yadav [Syncfusion]
Replied On May 29, 2019 04:27 AM UTC

Hi Carlos, 
 
Thanks for the update. We will wait to hear from you.  
 
Regards, 
Dinesh Babu Yadav 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon