Adding DynamicResource to GridTextColumn CellStyle

The requirement is that I have a DynamicResource that sets a theme styling on the xaml, specifically on the Foreground Color of a single GridTextColumn. Because the colors of the columns differ, I need to set it on each individual column.

While this code works...
```
<syncfusion:GridTextColumn
          MappingName="FolderName">
     <syncfusion:GridTextColumn.CellStyle>
          <Style TargetType="syncfusion:GridCell">
               <Setter Property="Foreground" Value="Red" />
          </Style>
     </syncfusion:GridTextColumn.CellStyle>
</syncfusion:GridTextColumn>
```


This throws an "System.InvalidCastException: Specified cast is not valid."
```
<syncfusion:GridTextColumn
          MappingName="FolderName">
     <syncfusion:GridTextColumn.CellStyle>
          <Style TargetType="syncfusion:GridCell">
               <Setter Property="Foreground" Value="{DynamicResource OurThemedTextColor}" />
          </Style>
     </syncfusion:GridTextColumn.CellStyle>
</syncfusion:GridTextColumn>
```

The "OurThemedTextColor" comes from a ResourceDictionary and works properly on all other elements.

Is there a way to achieve this through xaml?


PS: I am trying to avoid using the DataGrid_QueryCellStyle function if possible.

4 Replies 1 reply marked as answer

PK Pradeep Kumar Balakrishnan Syncfusion Team February 26, 2021 12:19 PM UTC

Hi Nitish Chauhan, 
 
Thank you using Syncfusion controls. 
 
We have checked the reported query “Application crashed with invalid cast exception when we define property to GridCell from Global resource using Style setter” we can reproduce the reported issue. Currently, we are validating the reported issue in source. We will validate and let you know the details by two business days March 2, 2021.  
 
We suggest you to use the following code to achieve the same requirement. 
<Application.Resources> 
        <ResourceDictionary> 
            <Color x:Key="Primary">Red</Color> 
        </ResourceDictionary> 
    </Application.Resources> 
 
<ContentPage.Resources> 
        <ResourceDictionary> 
            <local:GridStyleConverter x:Key="StyleConverter"/> 
        </ResourceDictionary> 
    </ContentPage.Resources> 
 
<sfgrid:SfDataGrid.Columns> 
        <sfgrid:GridTextColumn 
MappingName="OrderID"> 
            <sfgrid:GridTextColumn.CellStyle> 
                <Style TargetType="sfgrid:GridCell"> 
                    <Setter Property="Foreground" Value="{Binding ., Converter={StaticResource StyleConverter}, ConverterParameter={DynamicResource Primary}}" /> 
                </Style> 
            </sfgrid:GridTextColumn.CellStyle> 
        </sfgrid:GridTextColumn> 
    </sfgrid:SfDataGrid.Columns> 
 
public class GridStyleConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        if(value != null) 
        { 
            var resource = parameter as Xamarin.Forms.Internals.DynamicResource; 
            return Application.Current.Resources[resource.Key];        
        } 
 
        return Color.Black; 
    } 
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        return Color.Black; 
    } 
} 
We have also attached the sample for your reference in the following link. 
 
Regards, 
Pradeep kumar B

Marked as answer

PK Pradeep Kumar Balakrishnan Syncfusion Team March 2, 2021 12:36 PM UTC

Hi Nitish, 
 
Thank you for your patience. 
 
We suggest you to use previously shared work around as a solution for the reported issue. Please let us know if you have any concern on this. 
 
Regards, 
Pradeep Kumar B 



NC Nitish Chauhan March 4, 2021 02:48 PM UTC

Thank, this solution works!


KK Karthikraja Kalaimani Syncfusion Team March 8, 2021 06:58 AM UTC

Hi Nitish,

Thanks for the update. We glad to know that your issue has been resolved. Please get in touch if you required further assistance on this. 

Regards,
Karthik Raja

Loader.
Up arrow icon