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. Image for the cookie policy date

Override existing style in StyleSelector

Hi,

My question : I would like get access to "test" style in my GridCellRedEvo class !

Ex :
<Style x:Key="test" TargetType="grid:GridCell">
<Setter Property="Background" Value="PowderBlue" />
<Setter Property="FontWeight" Value="SemiBold" />
</Style>


<grid:GridNumericColumn HeaderText="CA" MappingName="EvolCaDif" TextAlignment="Right" FormatString="##0.0 %" Width="100" CellStyle="{StaticResource test}" CellStyleSelector="{StaticResource GridCellRedEvo}" >


public class GridCellRedEvo : StyleSelector
{
protected override Style SelectStyleCore(object item, DependencyObject container)
{

}

}

18 Replies

JG Jai Ganesh S Syncfusion Team September 7, 2016 01:03 PM UTC

Hi Aurélien, 
 
You can achieve your requirement to access the style in CellStyleSelector class like below, 
 
public class SelectorClass : StyleSelector 
    { 
        protected override Style SelectStyleCore(object item, DependencyObject container) 
        { 
 
            var data = item as BusinessObjects; 
            if (data != null) 
            { 
                if(data.EmployeeAge<45) 
                    return App.Current.Resources["test"] as Style; 
            } 
 
            return base.SelectStyleCore(item, container); 
        } 
    } 
 
 
 
Kb Link: 
 
 
Regards, 
Jai Ganesh S 



AU Aurélien September 7, 2016 02:27 PM UTC

I'll try this.
Thank you.


JG Jai Ganesh S Syncfusion Team September 8, 2016 12:41 AM UTC

Hi Aurélien,  
 
Thank you for the update. 
 
Please let us know if you need further assistance on this. 
 
Regards, 
Jai Ganesh S 



AU Aurélien September 9, 2016 08:19 AM UTC

Hi,
An other question !
How to know if test style is applied or not on the current cell ?


JG Jai Ganesh S Syncfusion Team September 12, 2016 12:33 PM UTC

 
Hi Aurélien,   
 
We have analyzed your query. In the last provided sample, we have applied the cell style using CellStyleSelector based on certain condition. So the test style will be applied in GridCell based on the condition. You can find if the currentcell contains the style or not by using the CurrentCellActivated event like below,   
 
this.datagrid.CurrentCellActivated += datagrid_CurrentCellActivated; 
 
void datagrid_CurrentCellActivated(object sender, CurrentCellActivatedEventArgs args) 
{ 
    GridCell cell; 
    Style cellStyle; 
    if (datagrid.SelectionController.CurrentCellManager.CurrentCell != null) 
    { 
        cell = (this.datagrid.SelectionController.CurrentCellManager.CurrentCell.ColumnElement as GridCell); 
        cellStyle = cell.Style; 
        if(cellStyle!=null) 
        { 
           //Write your code  
        } 
    } 
} 
 
 
Regards, 
Jai Ganesh S 
 



AU Aurélien September 12, 2016 12:37 PM UTC

Thank you.
I'll test that.

Aurélien


JN Jayaleshwari N Syncfusion Team September 13, 2016 06:23 AM UTC

Hi Aurélien, 
 
Thanks for the update.  
we will wait to hear from you 
 
Regards., 
 
Jayaleshwari N. 



AU Aurélien September 26, 2016 08:16 AM UTC

Hi,

Is there an other solution to test if the "test" style is applied on my current cell in my StyleSelector ?
I can't found a solution to access to the current cell in this !

My first need is to return a different style if the "test" style was applied or not, more a value test !

Thanks in advance.
Aurélien


JN Jayaleshwari N Syncfusion Team September 27, 2016 10:42 AM UTC

Hi Aurélien, 
 
We have checked your query “Access the current cell and test the test style applied or not”. You can get the GridCell using Container from StyleSelector. 
 
You can check the GridCell is CurrentCell or not by using DataColumnBase.IsCurrentCell property, like in below code. 
 
C# code: Return test style if the current cell doesn’t have style or the style is not a “test” style 
 
public class SelectorClass : StyleSelector 
{ 
    public override Style SelectStyle(object item, DependencyObject container) 
    { 
        var gridCell = container as GridCell; 
        if (gridCell == null) 
            return base.SelectStyle(item, container); 
 
        var dataColumnBase = gridCell.ColumnBase; 
        if(dataColumnBase.IsCurrentCell) 
        { 
            if (gridCell.Style == null) 
                return App.Current.Resources["test"] as Style; 
 
 
            if(gridCell.Style != null) 
            { 
                var setter = (gridCell.Style.Setters.FirstOrDefault(o => (o as Setter).Value.Equals("test")) as Setter); 
                var styleName = setter != null ? setter.Value : string.Empty; 
 
                if(string.IsNullOrEmpty(styleName.ToString())) 
                    return App.Current.Resources["test"] as Style; 
            } 
 
        } 
    } 

You can get the name of the style by using Name property of GridCell, like in below code. 

XAML code: Define a style with Name in setter of style. 
 
<Application.Resources> 
    <ResourceDictionary> 
        <Style x:Key="test" TargetType="Grid:GridCell"> 
            <Setter Property="Name" Value="test"/> 
            <Setter Property="Background" Value="PowderBlue" /> 
            <Setter Property="FontWeight" Value="SemiBold" /> 
        </Style> 
    </ResourceDictionary> 
</Application.Resources> 
 
Above code return the test style to the CurrentCell, where the GridCell does not have style and if the applied style is not a “test” style. 

Regards, 

Jayaleshwari N. 
 



AU Aurélien September 29, 2016 09:29 AM UTC

Hello,
Good solution but i have a problem.

if (dataColumnBase.IsCurrentCell) is always false !

Please see my test project in attachment.

Regards,
Aurélien

Attachment: App3_646aaeda.zip


JG Jai Ganesh S Syncfusion Team September 30, 2016 09:56 AM UTC

Hi Aurélien, 
 
The dataColumnBase.IsCurrentCell will be true only the currentcell was selected otherwise it is in false. Now we have modified your sample to apply the style in StyleSelector based on the cell style applied in cell. 
  
public class GridCellRedEvo : StyleSelector 
    { 
        protected override Style SelectStyleCore(object item, DependencyObject container) 
        { 
            var gridCell = container as GridCell; 
            if (gridCell == null) 
                return base.SelectStyle(item, container); 
 
            var dataColumnBase = gridCell.ColumnBase; 
           // if (dataColumnBase.IsCurrentCell) 
            {                 
                if (gridCell.Style == null) 
                    return App.Current.Resources["Redtest"] as Style; 
 
                if (gridCell.Style != null) 
                { 
                    var setter = (gridCell.Style.Setters.FirstOrDefault(o => (o as Setter).Value.Equals("testName")) as Setter); 
                    var styleName = setter != null ? setter.Value : string.Empty; 
 
                     
 
                    if (string.IsNullOrEmpty(styleName.ToString())) 
                        return App.Current.Resources["Redtest"] as Style; 
                } 
 
            } 
 
            return base.SelectStyleCore(item, container); 
        } 
    } 
 
 
Regards, 
Jai Ganesh S 



AU Aurélien October 5, 2016 07:09 AM UTC

Hello,

Thanks for your answer.
I have an other little problem with your example.
 (gridCell.Style != null)  is already null.

Regards,
Aurélien


AU Aurélien October 5, 2016 07:13 AM UTC

*answer=reply


JG Jai Ganesh S Syncfusion Team October 6, 2016 01:49 PM UTC

Hi Aurélien, 
 
In our last updated sample, we have applied the cell style (Redtest) while the gridCell.Style == null. Otherwise if the cell already has style then we have checked the style name whether is match with the already applied cell’s style name). If the style name equals with the already applied cell’s style, then we have applied the Redtest style. Could you please check the sample and let us know whether your requirement has been achieved otherwise please share the exact details for your requirement.  This would be more helpful for us to analyze further. 
 
Regards, 
Jai Ganesh S 



AU Aurélien October 7, 2016 07:12 AM UTC

Hello,
I need to know if test (CellStyle="{StaticResource test}") style is currently applied or not on the cell.

Your solution with name property in test style is a good solution but in the styleSelector gridCell.Style is always null.

Please see my test project in attachment.

Regards,
Aurélien

Attachment: App3_c516fb12.zip


JG Jai Ganesh S Syncfusion Team October 10, 2016 10:45 AM UTC

Hi Aurélien,  
It is not possible to find if the style is applied or not, for the current cell  in StyleSelector.  Because the cell style will be set based on the CellStyleSelector when you applied both CellStyle and CellStyleSelector in a column. Hence the gridcell.CellStyle always null in a StyleSelector. But the gridcell.Stlye  will be set only after you refreshing (Sorting, Filteing etc..) the columns. 
Regards, 
Jai Ganesh S 



AU Aurélien October 10, 2016 02:50 PM UTC

Hi,
Thanks for your reply.
I have found a other solution to resolve my problem.

Regards,
Aurelien


SP Sowndaiyan Paulpandi Syncfusion Team October 11, 2016 05:00 AM UTC

Hi Aurélien

Thanks for the update.

Please let us know if you need further assistance.

Regards,
 
Sowndaiyan  


Loader.
Up arrow icon