How to apply SelectedRowTextColor to the cells of a DataGridTemplateColumn in MAUI DataGrid (SfDataGrid)?
The current implementation of the MAUI SfDataGrid, the SelectedRowTextColor would not apply to the cells of DataGridTemplateColumn. This article illustrates how to apply SelectedRowTextColor
for DataGridTemplateColumn cells in a .NET MAUI DataGrid.
-
Initialize the SfDataGrid with the required properties, such as
ItemsSource
,SelectedRowTextColor
in theSfDataGrid.DefaultStyle
, and useDataGridTemplateColumn
for the required columns. -
Write a custom renderer class that extends from the
DataGridCellTemplateRenderer
class. In the overriddenOnSetCellStyle
method, check if the respective row is present inDataGrid.SelectedRows
. If it is, set theSelectedRowTextColor
for the content of theDataGridTemplateColumn
cell; otherwise, set theRowTextColor
to the text color of the content. -
To register the custom cell template renderer with the
SfDataGrid.CellRenderers
collection, remove the existing entry by its key and then add a new entry with the same key, along with an instance of the custom renderer.
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiApp1"
xmlns:dataGrid="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
x:Class="MauiApp1.MainPage">
<ContentPage.BindingContext>
<local:OrderInfoRepository x:Name="viewModel"/>
</ContentPage.BindingContext>
<dataGrid:SfDataGrid x:Name="dataGrid"
ItemsSource="{Binding OrderInfoCollection}"
SelectionMode="Single"
ColumnWidthMode="Auto">
<dataGrid:SfDataGrid.DefaultStyle>
<dataGrid:DataGridStyle SelectedRowTextColor="Red">
</dataGrid:DataGridStyle>
</dataGrid:SfDataGrid.DefaultStyle>
<dataGrid:SfDataGrid.Columns>
<dataGrid:DataGridTemplateColumn HeaderText="Order ID" MappingName="OrderID">
<dataGrid:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Text="{Binding OrderID}"/>
</DataTemplate>
</dataGrid:DataGridTemplateColumn.CellTemplate>
</dataGrid:DataGridTemplateColumn>
</dataGrid:SfDataGrid.Columns>
</dataGrid:SfDataGrid>
</ContentPage>
MainPage.xaml.cs
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
dataGrid.CellRenderers.Remove("Template");
dataGrid.CellRenderers.Add("Template", new CustomDataGridCellTemplateRenderer());
}
}
public class CustomDataGridCellTemplateRenderer : DataGridCellTemplateRenderer
{
protected override void OnSetCellStyle(DataColumnBase dataColumn)
{
base.OnSetCellStyle(dataColumn);
if (dataColumn?.DataGridColumn != null)
{
var gridStyle = this.DataGrid?.DefaultStyle;
DataGridCell gridCell = dataColumn.ColumnElement as DataGridCell;
if (this.DataGrid.SelectedRows.Any(row => row == dataColumn.RowData))
{
gridCell.TextColor = gridStyle?.SelectedRowTextColor;
Label sfLabel = dataColumn.ColumnElement?.Content as Label;
if (sfLabel != null)
{
sfLabel.TextColor = gridStyle?.SelectedRowTextColor;
}
}
else
{
gridCell.TextColor = gridStyle?.RowTextColor;
Label sfLabel = dataColumn.ColumnElement?.Content as Label;
if (sfLabel != null)
{
sfLabel.TextColor = gridStyle?.RowTextColor;
}
}
}
}
}
}
Output
Download the complete sample from GitHub
Conclusion
I hope you enjoyed learning how to apply SelectedRowTextColor to the cells of a DataGridTemplateColumn in MAUI DataGrid.
You can refer to our .NET MAUI DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. Explore our .NET MAUI DataGrid example to understand how to create and manipulate data.
For current customers, check out our components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!