Search code examples
wpfxamlwpfdatagrid

Is There A Way To Style A WPF DataGrid NewItem Placeholder


I have a simple data control defined as follows

<DataGrid x:Name="ScheduleDataGrid" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" Margin="5" ItemsSource="{Binding ScheduleItems}">
    <DataGrid.Columns>

        <DataGridTextColumn...></DataGridTextColumn>
        ....
        <DataGridTextColumn...></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

I have specified CanUserAddRows="True". As advertised, this puts a blank row at the bottom of the grid for the user to add a new row.

Is there a way to style that new item placeholder independently of the rest of the grid?


Solution

  • Yes it is possible to change that placeholder row.

    Take a look at this example I just made.

    <Window.Resources>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding}" Value="{x:Static CollectionView.NewItemPlaceholder}">
                    <Setter Property="Background" Value="Yellow"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <DataGrid AutoGenerateColumns="False" CanUserAddRows="True" ItemsSource="{Binding List}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Test" Binding="{Binding Path=Name, Mode=TwoWay}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
    

    That placeholder row will appear with yellow background.