tags:

views:

371

answers:

1

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.

A: 

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.

Gurdas Nijor
Tried it, doesn't work :-(. I think that the problem is is the collection rather then in the DataGrid itself. ( I tries List<T> with the same result.
Jan Kratochvil
Whoops, solved the wrong bug! Get view.CanAddNew before you set it as the itemssource. Something about accessing it before associating it with a DataGrid fixed this issue for me.
Gurdas Nijor
You can just use a dummy variable like (bool dummy = view.CanAddNew)
Gurdas Nijor
Really? I just tried it and it didn't fix the issue. Could you copy the code for me? Thx
Jan Kratochvil
See my edit; sorry, I really need to read your code before I start debugging it!!! (I guess the same applies to my own)
Gurdas Nijor
Thanks a lot for the help, I just implemented it and it's working. I'm one interface smarter again :D
Jan Kratochvil
You do not need to implement IEditableObject. See: http://www.codeproject.com/KB/WPF/MVVM_DataGrid.aspx
JP