0
votes

I have a DataGrid with ItemsSource bound to <ObservableCollection<MyItem>()

MyItem is a class with just two public strings. It has a default constructor (sets Text1 to something and Text2 to something (i.e "test1" and "test2").

For the DataGrid, I set CanUserAddRows to True and IsReadOnly is False.

Yet the DataGrid is not properly adding blank row. I mean it always opens with single blank row at the bottom, but this additional blank row is not set with default values - those I left in MyItem's default constructor). Also the entire row is kind of like nulled (?). I know that, because I have some XAML bindings to background / foreground cell colors and - on rows (MyItems) I added manually, before I bind the entire ObservableCollection to DataGrid) - those color-bindings work fine. On this odd new blank row, they are failing (saying that they "...can't cast MS.Internal.Namedobject...").

There is one more issue - when I start editing this weird blank row, there is never a new row added automatically at the bottom of DataGrid. Not even after pressing Enter, after in-cell text editing. In WinForms, new blank row always appeared automatically when at least one cell was touched (not even value changed, immediately after click on a blank cell).

What am I missing?

2
Please provide with the code for theMyItem class and the xaml for the bindingRomka

2 Answers

0
votes
public class MyItem
{
   public string Text1 { get; set; }
   public string Text2 { get; set; }

   public MyItem()
   {
       Text1 = "test1";
       Text2 = "test2";
   }
}

/* ... XAML ... */

<DataGrid x:Name="dataGrid"
                  AutoGenerateColumns="False"
                  CanUserAddRows="True"
                  CanUserDeleteRows="True"
                  CanUserReorderColumns="False"
                  CanUserResizeColumns="False"
                  CanUserResizeRows="False"
                  CanUserSortColumns="False"
                  CellStyle="{StaticResource dataGridCell}"
                  SelectionMode="Single"
                  SelectionUnit="FullRow">
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="1*">
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Text1}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Text1}"
                                     TextChanged="TextBox1_TextChanged" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Width="1*">
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Text2}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Text2}"
                                     TextChanged="TextBox2_TextChanged" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

0
votes

I found the reason. For DataGridTemplateColumn, both CellTemplate and CellEditingTemplate cannot be TextBoxes. When I changed CellTemplate to TextBlock, it started working fine.