Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I just want to to set background of a cell to Red.
I added a rectangle in the cell and change the background to pink.
But I don't why my code does not work.
Any help please?

P.S: Accidentally I changed the Height of rectangle to 10, and I saw there appear a little pink band in the cell !!!! But I couldn't understand what is it!!!!

What I have tried:

XML
<DataGridTemplateColumn Header="MyHeader" IsReadOnly="True" Width="2*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Rectangle Fill="Pink" Height="Auto"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Posted
Updated 31-Dec-23 23:59pm
v3

 
Share this answer
 
Comments
Sh.H. 1-Jan-24 6:30am    
Thanks. This example has TextBlock. I do not want to have any element in the cell. Beside, I followed your suggestion and I used TextBlock as the example. There appears just a little triangle at the middle of cell.
Anyway... doesn't work.
Your Rectangle has nothing to use for a "size request" ... at a minimum, give the Rectangle a .MinHeight and a .MinWidth in this case (e.g. 24x24 or 32x32). If the Rectangle was "contained", and "stretched", you wouldn't need to specify dimensions, but there are no other controls in the template.

(I say .Min... because some controls have a default .Min..., and anything less, won't work if specified via .Width or .Height only).
 
Share this answer
 
v2
Comments
Sh.H. 2-Jan-24 16:41pm    
@Gerry Schmitz
Thanks.
You are right. If I use min or max, it works. But I do not want to show the rectangle as min. I need the rectangle to be stretch. But it doesn't work in the code.
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:

XML
<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:
C#
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);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900