You do not need to set the size of the
Rectangle
if you want to fill the
Grid
area.
<Grid Width="10" Height="20">
<Rectangle Fill="Red"></Rectangle>
</Grid>
Now, if you remove the
Width
and
Height
of the
Grid
, the
Grid
will fill the parent space and the Rectangle will fill the Grid. if the Grid's parent is the form, then the background of the form will be filled with both the
Grid
and the
Rectangle
with the
Red
fill.
<Window x:Class="WpfGridRect.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Rectangle Fill="Red"></Rectangle>
</Grid>
</Window>
Most, not all, will fill the available area.
StackPanel
is an exception unless you tell it to.
UPDATE
Nowhere in the question asked does it stipulate that the parent was a DataGridCell. However, the same principle applies:
<DataGridTemplateColumn Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Rectangle Fill="DeepSkyBlue"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
The
Rectangle
will fill the
DataGridTemplateColumn
.
UPDATE #2
Again, the requirements change. However, the same principle applies. Here is my modified answer from your other question on
setting the background color of a cell[
^]. I've added using a Rectangle for the background color, which, in my opinion, is the wrong way of doing it.
<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>
<DataGridTemplateColumn Header="Last Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Rectangle Fill="DeepSkyBlue"/>
<TextBlock Text="{Binding Path=LastName}" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</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);
This is a working example. Create a new project called
WpfDataGridCellBackground (I am using .Net Core WPF project), then copy and paste the code and run.
Here is an image of the app running:
Wpf DataGrid Cell Background Color - Imagebin[
^]