Changing Label color in sfDataGrid DataTemplate

Hi,

I have a sfDataGrid binding to a Dictionary in my ViewModel adding GridTemplateColumns with a DataTemplate with a label
programmatically as follows:


            var model = vm.DynamicCollection.FirstOrDefault();
            foreach (KeyValuePair kvp in model.Values.ToArray())
            {
                var templateColumn = new GridTemplateColumn()
                {
                    HeaderText = kvp.Key,
                    MappingName = $"Values[{kvp.Key}]",
                    AllowSorting = true,
                    TextAlignment = TextAlignment.Center
                };


                var dataTemplate = new DataTemplate(() =>
                {
                    var label = new Label()
                    {
                        VerticalOptions = LayoutOptions.Center,
                        HorizontalOptions = LayoutOptions.Center
                    };
                    label.SetBinding(Label.TextProperty, $"Values[{kvp.Key}]");
                    //Highlight current user row
                    //label.SetBinding(Label.BackgroundColorProperty, ".", BindingMode.Default, backgroundColorConverter);

                    return label;
                });
                templateColumn.CellTemplate = dataTemplate;
                dataGrid.Columns.Add(templateColumn);
            }

I am also subscribing to QueryCellStyle event to change Label color. The problem is that e.Style.ForegroundColor property doesn't seem to be changing
my label color. I did not find a way to get a reference to the label from QueryCellStyle event handler. e.StyleForegroundColor works fine when adding GridTextColumns (not GridTemplateColumn). 

dataGrid.QueryCellStyle += DataGrid_QueryCellStyle;

        private void DataGrid_QueryCellStyle(object sender, QueryCellStyleEventArgs e)
        {
          //Some business logic here to define color
            e.Style.ForegroundColor = Color.Green;
            e.Handled = true;
        }

Any suggestions on how to change my label color inside the DataTemplate? 

I'm attaching my sample project.

Attachment: DataGridDemo_fec606dc.zip

3 Replies 1 reply marked as answer

KK Karthikraja Kalaimani Syncfusion Team May 10, 2021 04:54 AM UTC

Hi Adolfo, 

We cannot change the foreground color of the label using the QueryCellStyle event because that label is loaded in DataTemplateColumn. Since the DataTemplateColumn can load any type of Xamarin forms views. However, we can achieve your requirement by getting the current rows from RowGenerator.  please refer to the below code snippets. 

Code snippets: 

  private void DataGrid_QueryCellStyle(object sender, QueryCellStyleEventArgs e) 
        {
            if (e.RowIndex != 0)
            {
                var datarow = this.dataGrid.GetRowGenerator().Items.FirstOrDefault(x => x.RowIndex == e.RowIndex);
                var WholeRowElement = (VirtualizingCellsControl)datarow.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name.Equals("WholeRowElement")).GetValue(datarow);
                if (WholeRowElement is VirtualizingCellsControl)
                {
                    for( int i=0;i< WholeRowElement.Children.Count;i++)
                    {
                        if ((WholeRowElement.Children[i] as GridCell).Content is Label)
                        {
                            ((WholeRowElement.Children[i] as GridCell).Content as Label).TextColor = Color.Red;
                        }
                    }
                }
            }
        }

Regards,
Karthik Raja 


Marked as answer

AD Adolfo May 10, 2021 10:27 AM UTC

Perfect thank you!


KK Karthikraja Kalaimani Syncfusion Team May 11, 2021 05:25 AM UTC

Hi Adolfo, 

Thanks for the update. We glad to know that the provided solution has meets your requirement. Please let us know if you need further assistance from us. 

Regards,
Karthik Raja


Loader.
Up arrow icon