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
close icon

Trying to set a cell a specific color based upon data value and getting an error.

I am using the following code to check a value and set a cell a specific color:

<Application.Resources>

        <DataTemplate x:Key="CellTemplate1">
            <TextBlock Foreground="DarkBlue" Text="{Binding Path=Value}" />
        </DataTemplate>

        <DataTemplate x:Key="CellTemplate2">
            <TextBlock Foreground="DarkRed" Text="{Binding Path=Value}" />
        </DataTemplate>

    </Application.Resources>

    <Page.Resources>
        <local:CellTemplateSelector x:Key="cellTemplateSelector" />
    </Page.Resources>

c#
public class CellTemplateSelector : DataTemplateSelector
        {
            protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
            {
                if (item != null)
                {
                    var data = (item as DataContextHelper).Record as orderHeader;

                    if (data.ORDERSTATUS == "0")
                        return Application.Current.Resources["CellTemplate1"] as DataTemplate;

                    else
                        return Application.Current.Resources["CellTemplate2"] as DataTemplate;
                }

                return base.SelectTemplateCore(item, container);
            }
        }

I am getting an error on <Application.Resources> - The member "Resources" is not recognized or is not accessible" and on <local:CellTemplateSelector x:Key="cellTemplateSelector" /> The name "CellTemplateSelector" does not exist in the namespace "using:HDW_Order" 

I am also attaching the xaml file

Attachment: custDetails_d504a4d7.zip

3 Replies

EM Elavarasan M Syncfusion Team June 27, 2016 10:55 AM UTC

Hi Garling, 

Thank you for contacting Syncfusion support. 

We have analyzed your query and prepared the sample based on your code, its working in our side without any exception and able to set the custom templates using CellTemplateSelector like below code example, 

<Application.Resources> 
    <DataTemplate x:Key="CellTemplate1"> 
        <TextBlock Foreground="DarkBlue" Text="{Binding Path=Value}" /> 
    </DataTemplate> 
 
    <DataTemplate x:Key="CellTemplate2"> 
        <TextBlock Foreground="DarkRed" Text="{Binding Path=Value}" /> 
    </DataTemplate> 
</Application.Resources> 
 
<Page.Resources> 
        <local:CellTemplateSelector x:Key="templateSelector"/> 
</Page.Resources> 
 
<syncfusion:GridTextColumn CellTemplateSelector="{StaticResource templateSelector}" ColumnSizer="SizeToHeader" TextWrapping="NoWrap"  HeaderText="OrderID" MappingName="OrderID" SetCellBoundValue="True" /> 

public class CellTemplateSelector : DataTemplateSelector 
{ 
    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) 
    { 
        if (item != null) 
        { 
            var data = (item as DataContextHelper).Record as OrderInfo; 
 
            if (data.OrderID < 1005) 
                return Application.Current.Resources["CellTemplate1"] as DataTemplate; 
 
            else 
                return Application.Current.Resources["CellTemplate2"] as DataTemplate; 
        } 
 
        return base.SelectTemplateCore(item, container); 
    } 
} 


 
 
Note: We suspect that you are created the TemplateSelector in another namespace. Could you please ensure whether the TemplateSelector is present in “HDW_Order” namespace, according to your code. 

You can refer the below UG to know more about the TemplateSelectors, 

 
Regards, 
Elavarasan M


GB Garling Beard June 29, 2016 01:24 PM UTC

Thank you, I have it working now. 


EM Elavarasan M Syncfusion Team June 30, 2016 05:14 AM UTC

Hi Garling, 

Thanks for your update. 

Regards, 
Elavarasan M

Loader.
Live Chat Icon For mobile
Up arrow icon