Default text for new row

Sep 3, 2008 at 6:30 PM
Is there a way to put text in the row used to add new items?  My DataGrid shows an empty row at the bottom that the user can click on to add a new row.  Is there a way to make this row display something like "Click here to add a new row"?
Coordinator
Sep 3, 2008 at 9:50 PM
Hi.  The new item placeholder row’s data item is CollectionView.NewItemPlaceholder.  You can achieve your scenario by re-templating the row when its data item is CollectionView.NewItemPlaceholder.  In the final version, we are also going to make the DataGrid.NewItemPlaceholder public (it's currently internal), which will allow you to re-template individual cells in the NewItemPlaceholder.  Also, if you'd like to supply some default values for the new row when it's in editing mode (after the user has clicked on it) you can add a listener for the InitializingNewItem event on the DataGrid and offer the default values for the row when the event is raised.  Thanks!
Sep 4, 2008 at 5:16 PM
I do not see how to template the NewItemPlaceholder since it is not exposed.  Would it be possible for you to post some code/xaml to get me started?
Thanks.
Coordinator
Sep 10, 2008 at 7:45 PM

There should be a workaround to this issue using RowStyleSelector (some sample code below), but after some investigation, we've discovered that there is a bug in the CTP which prevents you from successfully retemplating the row with CollectionView.NewItemPlaceholder as it's content.  This issue will be fixed in v1, at which time the code below should work.  Thank you for helping us find this bug!

<Style x:Key="defaultRowStyle" TargetType="{x:Type dg:DataGridRow}">

</Style>


<
Style x:Key="newItemRowStyle" TargetType="{x:Type dg:DataGridRow}">

                <Setter Property="Template">

                    <Setter.Value>

                        <ControlTemplate TargetType="{x:Type dg:DataGridRow}">

                            <Border x:Name="DGR_Border"

                                  Background="{TemplateBinding Background}"

                                  BorderBrush="{TemplateBinding BorderBrush}"

                                  BorderThickness="{TemplateBinding BorderThickness}"

                                  SnapsToDevicePixels="True">

                                <dg:SelectiveScrollingGrid>

                                    <Grid.ColumnDefinitions>

                                        <ColumnDefinition Width="Auto"/>

                                        <ColumnDefinition Width="*"/>

                                    </Grid.ColumnDefinitions>

 

                                    <Grid.RowDefinitions>

                                        <RowDefinition Height="*"/>

                                        <RowDefinition Height="Auto"/>

                                    </Grid.RowDefinitions>

 

                                    <TextBlock Text="Click here to add a new item." Grid.Column="1"/>                                   

 

                                    <dg:DataGridRowHeader dg:SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"  Grid.RowSpan="2"

                                    Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type dg:DataGrid}}, Path=HeadersVisibility, Converter={x:Static dg:DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static dg:DataGridHeadersVisibility.Row}}"/>

                                </dg:SelectiveScrollingGrid>

                            </Border>

                        </ControlTemplate>

                    </Setter.Value>

                </Setter>

            </Style>

 

<dg:DataGrid.RowStyleSelector>

                <local:RowStyleSelector DefaultStyle="{StaticResource defaultRowStyle}"

                                        NewItemStyle="{StaticResource newItemRowStyle}" />

            </dg:DataGrid.RowStyleSelector>

 

public class RowStyleSelector : StyleSelector

    {

        public Style DefaultStyle { get; set; }

        public Style NewItemStyle { get; set; }

 

        public override Style SelectStyle(object item, DependencyObject container)

        {

            ItemsControl itemsControl = ItemsControl.ItemsControlFromItemContainer(container);

            if (item == CollectionView.NewItemPlaceholder)

            {

                return NewItemStyle;

            }

            else

            {

                return DefaultStyle;

            }

        }

    }

Oct 19, 2009 at 8:54 PM
Edited Oct 19, 2009 at 9:08 PM

when is V1 coming out?. I am trying to get my selected row to be rendered using certain style. Is there another way to achieve this?

Mar 2, 2010 at 6:29 AM

Hi SamanthaMSFT,

You have said that the DataGrid.NewItemPlaceholder should be public in the final release. But it is still internal in the VS2010 RC. Will it become public in the VS2010 RTM?

Thanks,

Tony

May 19, 2010 at 6:38 PM

The DataGrid.NewItemPlaceholder is still not public in the final release. If that class can't be accessed what is the proposed way to style the NewItemPlaceholder-row?

 

Regards,

Gauss

Sep 8, 2010 at 12:21 PM

This is a real pain. Can we have a fix to allow us to style the new row please.