dotnet / dotnet/wpf

No "top-left cell" in DataGrid with custom RowHeader template but no actual rows

Open
#11,399 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

I have a custom class derived from `DataGrid` which adds a template for the "new item placeholder row" header. However, if the `ItemsSource` is empty, there is no "top-left cell" in the grid so the actual first column header appears above that placeholder:

Image

It's only after I add a row that the full column header is shifted to the right, leaving space for the "top-left cell":

Image

This layout then persists even when I remove the row:

Image

Is that by design or a bug? Could I force the grid to create the "top-left cell" even when empty?

### Reproduction Steps

This is the style I use for my `DataGridWithAddButton` control.

```

<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Converter={converters:HasAncestorOfTypeConverter}, ConverterParameter={x:Type localControls:DataGridWithAddButton}}" Value="True" />
<Condition Binding="{Binding CanUserAddRows, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Value="True" />
<Condition Binding="{Binding IsNewItem, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Value="True" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Button
Command="{Binding AddItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=localControls:DataGridWithAddButton}}"
DataContext="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}">
<Path Data="{StaticResource Geometry.Plus}" Style="{StaticResource Theme.Path.Style.Small}" />
</Button>
</DataTemplate>
</Setter.Value>
</Setter>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>


<Setter Property="RowHeaderStyle" Value="{StaticResource DataGrid.RowHeader.Style.WithAddButton}" />
<Setter Property="CanUserAddRows" Value="True" />
<Setter Property="HeadersVisibility" Value="All" />

```

### Expected behavior

The "top-left cell" is created if a new row placeholder is visible and its row header is not empty.

### Actual behavior

No "top-left cell" is created, making the layout inconsistent between "no rows were created" and "rows were created but then deleted".

### Regression?

_No response_

### Known Workarounds

_No response_

### Impact

_No response_

### Configuration

.NET 10, Windows 11 Pro 25H2

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.