Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'EditItem' is not allowed for this view - databinding issue

I am trying to do data binding in WPF on a data grid using a custom list. My custom list class contains a private data list of type List<T>. I cannot expose this list, however the indexers are exposed for setting and getting individual items. My custom class looks like this:

public abstract class TestElementList<T> : IEnumerable
        where T : class
{
    protected List<T> Data { get; set; }
    public virtual T Get(int index)
    {
        T item = Data[index];
        return item;
    }

    public virtual void Set(int index, T item)
    {
         Data[index] = item;
    }
...
}

The data is binded but when I try to edit it, I get 'EditItem' is not allowed for this view error. On doing extensive searching over web, I found that I might need to implement IEditableCollectionView interface also. Can anybody please help me to either give pointers on how to implement this interface or any suggest any other better way to do databinding on custom list?

like image 877
Scooby Avatar asked Jun 08 '10 23:06

Scooby


3 Answers

Though I am not fully understanding your requirement, do you think using an ObservableCollection will solve your issue?

public abstract class TestElementList<T> : ObservableCollection<T>
    where T : class
 {
   public virtual T Get(int index)
   {
     T item = this[index];
     return item;
   }

   public virtual void Set(int index, T item)
   {
     this[index] = item;
   }
 ...
}
like image 163
Jobi Joy Avatar answered Nov 15 '22 05:11

Jobi Joy


I had the same exception. It seems that you have to bind do IList. I was binding to a IEnumerable and this exception was thrown.

like image 21
Mário Meyrelles Avatar answered Nov 15 '22 06:11

Mário Meyrelles


Just to add my own observation. I had a datagrid with specifically defined columns in Xaml and its ItemsSource set to a simple dictionary. When I tried to edit the second column, I got this exception referring to the dictionary. I then set the data grid ItemsSource to a list of the Keys (dataGrid.Keys.ToList()). I could then edit the second column. It seems a list view allows an 'EditItem'.

edit: Did a little bit more digging into this. I set up a BeginningEdit handler and started poking around. One thing I noticed was that every single time I got this error, EditingEventArgs.Source was a Border. If I can find the time, I may look into this one down a bit further. Also, on one instance, my converting the dictionary keys to a List did not work. I had to convert it to an Observable collection, despite the fact that a List was suitable in all other places in my code where I was doing essentially an identical type of assignment.

edit again: Ok, I have another fix for those for which using an IList type doesn't work. Attach a BeginningEdit handler to your DataGrid and point to this code:

    private void Grid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {
        //// Have to do this in the unusual case where the border of the cell gets selected
        //// and causes a crash 'EditItem is not allowed'
        e.Cancel = true;
    }

This will only hit if you somehow manage to physically tap down on the border of the cell. The event's OriginalSource is a Border, and I think what my happen here is instead of a TextBox, or other editable element being the source as expected, this un-editable Border comes through for editing, which causes an exception that is buried in the 'EditItem is not allowed' exception. Canceling this RoutedEvent before it can bubble on through with its invalid Original Source stops that error occurring in its tracks.

Glad to have found this as there was, in my case, a DataGrid on which I couldn't use an IList type.

like image 42
ouflak Avatar answered Nov 15 '22 07:11

ouflak