Why does one of MY WPF DataGrids give the “'EditItem' is not allowed for this view” exception?

不羁岁月 提交于 2019-12-06 14:17:36

I have also this problem, And found that the point here is that we can not edit a IEnumerable in a DataGrid, only a list can be edited.

therefore we didn't need to create a new class, its works also on a LINQ query with anonymous return type. it's need only to be a list.

here is a sample of my code:

 dtgPrdcts.ItemsSource= ProductLists.Select(Function(x) New With {.ListTitle = x.ListTitle, .ProductID = x.ProductID, .License = "", .ForRemove = True}).ToList

I still don't know what specifically caused the problem, but I managed to work around it, and I'm not sure how much of what I did was overkill, but it works.

I created a new class just for the purpose of holding the data in the DataGrid rows. I make a List of objects of this class and fill it in and bind it to the DataGrid as I was doing before. I also added the usual stuff and nonsense for getting Change Notification to work (probably overkill) and I had to re-define a comparison function in a different way to get it to sort because of that whole comedy situation.

i.e.

List<UsablePref> MemberIngredientPrefs = new List<UsablePref>();

...

            foreach (RAM_Ingredient ingredient in App.Ingredients)
        {
            ingredient.GetPreferences(EditorMember);
            UsablePref pref = new UsablePref();
            pref.Ingredient = ingredient.Ingredient;
            pref.IngredientID = ingredient.IngredientID;
            pref.Preference = ingredient.Preference;
            MemberIngredientPrefs.Add(pref);
        }

        // Sort alphabetically by ingredient name, 
        MemberIngredientPrefs.Sort(UsablePref.CompareByName);
        // and bind the ingredient prefs DataGrid to its corresponding List
        dgIngredients.DataContext = MemberIngredientPrefs;

I had this same problem trying to create a list of rows from a join; since the LINQ query returns an IEnumerable, I had the DataGrid bound to that IEnumerable; this worked fine for readonly and oddly worked with ComboBoxes and some other custom controls I used, but plain text editing threw the InvalidOperationException. The solution was an ObservableCollection in place of the IEnumerable; basically from:

BoundView = (/*LINQ QUERY*/); // is IEnumerable<CustomJoinObject>

to

BoundView = new ObservableCollection<CustomJoinObject>(/*LINQ QUERY*/);

In both cases BoundView is the DataContext for the DataGrid.

I'm assuming this happens because IEnumerable doesn't have the machinery to support a datagrid, whereas ObservableCollection does.

Stenio Dinart

The model class needs to implement the interface INotifyPropertyChanged coming from the namespace System.ComponentModel.

Class example:

 public class Exemple :  INotifyPropertyChanged
 {
   #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion INotifyPropertyChanged Members

 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!