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