You do not need a Rectangle to set the background color of a cell, you just need to set the Cell Style. Here is a working example:
<Window x:Class="WpfDataGridCellBackground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<SolidColorBrush x:Key="clBr" Color="Red" />
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Path=.}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="First Name"
Binding="{Binding Path=FirstName}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background"
Value="{StaticResource clBr}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Last Name"
Binding="{Binding Path=LastName}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
And the code-behind:
using System.Windows;
namespace WpfDataGridCellBackground;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new List<Person>()
{
new("Freddie", "Mercury"),
new("John", "Lennon")
};
}
}
public record Person(string FirstName, string LastName);