Jan Kratochvil
Jan Kratochvil

Reputation: 2327

WPF DataGrid - row for new entry not visible

The problem is, that the blank row in the DataGrid isn't appearing, ergo user can not add data. Here is the code:

System.Collections.ObjectModel.ObservableCollection<CoreVocabularyEntry> dataList = new System.Collections.ObjectModel.ObservableCollection<CoreVocabularyEntry>();
    public VocabularyToolWindow()
    {
        InitializeComponent();
        dataList.Add(new CoreVocabularyEntry { Foreign = "ja", Native = "ano" });
        ListCollectionView view = new ListCollectionView(dataList);
        WordsDataGrid.ItemsSource = dataList;
        WordsDataGrid.CanUserAddRows = true;
        MessageBox.Show(view.CanAddNew.ToString());
    }

I can't figure out why view.CanAddNew equals false. This looks like a pretty standart scenario, so there's probably something obvions I'm missing. Can someone tell me what is wrong with the code ? CoreVocabularyEntry is just the following:

public struct CoreVocabularyEntry : IVocabularyEntry
{
    #region IVocabularyEntry Members

    public string Foreign
    {
        get;
        set;
    }

    public string Native
    {
        get;
        set;
    }

    #endregion
}

Thx, J.K.

Upvotes: 3

Views: 5418

Answers (2)

kyjote
kyjote

Reputation: 121

This may be more simple than above. I had this problem when I did NOT have a default constructor and therefore the datagrid did not know how to add the new row. My only constructor involved a bunch of information to be supplied. Go to the object of that the List<> is made from and add a constructor like: public MyClass(){} Hope this helps!

Upvotes: 7

gn22
gn22

Reputation: 2086

Move WordsDataGrid.CanUserAddRows = true; above the statement where you set the DataGrid's ItemSource.

EDIT:

Just noticed you didn't implement IEditableObject. You'll need to do that for using the editing features of the DataGrid.

Upvotes: 1

Related Questions