How to override Theme style based on content

I am trying to override the foreground property of a text box in a SfDataGrid. This was working before I applied the Office2019Black theme. The style selector gets called and returns the appropriate style but the box text doesn't change color.
public class StockSelectorClass : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
double value = 0;
if (item is StockSum data)
{
var column = (container as GridCell).ColumnBase.GridColumn.MappingName;
switch (column)
{
case "Stock.PriceAndChange":
case "DailyGain":
value = data.Stock.Diff;
break;
case "YTDGAIN":
value = data.YTDGain;
break;
case "PriceGain":
value = data.PriceGain;
break;
case "NetGain":
value = data.NetGain;
break;
case "YTD":
value = data.YTD;
break;
case "YTDGain":
value = data.YTDGain;
break;
case "CAGR":
value = data.CAGR;
break;
default:
break;
}
}
if (value < 0)
return App.Current.Resources["RedCellStyle"] as Style;
if (value > 0)
return Application.Current.Resources["BlueCellStyle"] as Style;
return base.SelectStyle(item, container);
}
}
And in the App.xaml file I have:
For the SfDataGrid I am applying the CellStyleSelector as: CellStyleSelector="{StaticResource stockstyleselector}"
Looking for any help you can give.
Thanks in advance.

3 Replies 1 reply marked as answer

SS Susmitha Sundar Syncfusion Team August 21, 2020 06:04 PM UTC

Hi Tom, 
 
Thank you for using Syncfusion controls. 
Your requirement “How to override Theme style based on content” can be achieved by added the style with in ResourceDictionary. 
 
 
<Application.Resources> 
        <ResourceDictionary> 
            <ResourceDictionary.MergedDictionaries> 
                <ResourceDictionary Source="/Syncfusion.Themes.Office2019Black.WPF;component/SfDataGrid/SfDataGrid.xaml" /> 
            </ResourceDictionary.MergedDictionaries>             
    <local:SelectorClass x:Key="styleSelector"/> 
    <Style x:Key="redCellStyle" BasedOn="{StaticResource SyncfusionGridCellStyle}" TargetType="syncfusion:GridCell"> 
            <Setter Property="Foreground" Value="Red" /> 
        </Style> 
    <Style x:Key="blueCellStyle" BasedOn="{StaticResource SyncfusionGridCellStyle}" TargetType="syncfusion:GridCell"> 
            <Setter Property="Foreground" Value="DarkBlue" /> 
        </Style> 
    </ResourceDictionary> 
</Application.Resources> 
 
 
 
Please check the sample and let us know if you need further assistance on this. 
 
Regards, 
Susmitha S 



PR Padmini Ramamurthy Syncfusion Team August 24, 2020 05:15 AM UTC

  
From: Tom Borgoyn 
Sent: Sunday, August 23, 2020 4:39 PM
To: Syncfusion Support <[email protected]>
Subject: FW: Syncfusion support community forum 157114, How to override Theme style based on content, has been updated. 
  
Never mind. I got it working. 
Thanks for your help. 

From: Tom Borgoyn
Sent: Friday, August 21, 2020 6:27 PM
To: 'Syncfusion Support' <[email protected]>
Subject: RE: Syncfusion support community forum 157114, How to override Theme style based on content, has been updated. 

Thanks for your quick response. I am using .NET core 3.1 and the Source line is not finding the package. Do you have an example for .net core? 



VS Vijayarasan Sivanandham Syncfusion Team August 25, 2020 03:11 PM UTC

Hi Tom Borgoyn,

Thanks for the update.

Based on provided information we have prepared the sample for achieve your requirement.
 
We hope this helps. Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S
 


Marked as answer
Loader.
Up arrow icon