wpfdatagridcontrols

Cannot set the CanUserAddRows property of the DataGrid in WPF


I have a DataGrid in a WPF Window like this:

<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" 
          Name="dataGrid1" VerticalAlignment="Stretch" CanUserAddRows="True" 
          CanUserDeleteRows="True" >
    <DataGrid.Columns>
        <DataGridTextColumn Header="X"></DataGridTextColumn>
        <DataGridTextColumn Header="Y"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

When I go to the properties panel of the DataGrid and try to check the CanUserAddRows property, it remains unchecked! however it adds the CanUserAddRows="True" to the XAML. Same things happens with the CanUserDeleteRows property.

What is going on?


Solution

  • To check if I understand the question correctly: even though you set the CanUserAddRows="True" property in XAML, when you check in the code behind, the same property is False.

    It's likely that you are binding to a read-only collection of some kind. Make sure you are binding to something like ObservableCollection<T> to make these features work.

    If there is no source specified at all, the DataGrid won't be able to create new rows into it.