简体   繁体   中英

How can I add a template for datagrid's NewItemPlaceholder row?

I have implemented a datagrid in my WPF application using the MVVM design pattern. Each row of the datagrid has a combobox and a another control based on the selection of the combobox and everything works fine.
The problem is that the NewItemPlaceholder (the row that enables me to add new objects to my ObservableCollection) by default displays DataGrid.NewItemPlaceholder.
As I have read here the problem is that I must create a datatemplate for that row. And in that link it is described how to do it programatically.
Is there a way to do it directly in XAML?
Thank you in advance.

Try this out:

    <DataGrid.RowHeaderTemplate>
            <DataTemplate>
                <buttons:CloseButton x:Name="CloseButton"/>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                  AncestorType={x:Type DataGridRow}}, 
                                  Path=IsNewItem}" Value="True">
                        <Setter TargetName="CloseButton" Property="Visibility" Value="Collapsed"></Setter>
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </DataGrid.RowHeaderTemplate>

I use a trigger to hide the button, only on the New Item row. With a little ingenuity I ought to be able to swap out entire templates, like use a content presenter and change its template or something similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM