I read all the Q&A that I could find here and on the MS forums about this exception, and tried most of the suggestions I understood, and several others. It seems that this exception can occur for a number of reasons.
As with others, I have a WPF DataGrid binding to a collection that throws this exception when you try to edit one of the cells. They are configured for write capabilities, the collection is ObservableCollection, I implemented get and set handlers that send notifications.
The suggestions that I have not tried are those related to the implementation of the non-original IList interface, because I have no idea what I will do for this. In addition, I have many DataGrids linked to various lists and collections in my application that work, and this one was used when it was associated with the LINQ collection.
Please help me figure out what I need to do here.
Data Grid:
<DataGrid Name="dgIngredients" Margin="567,32,0,44" Width="360" ItemsSource="{Binding}" IsReadOnly="False"
AutoGenerateColumns="False" HorizontalAlignment="Left" CanUserAddRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTextColumn Width="63" Header="Percent" Binding="{Binding Preference}" IsReadOnly="False" />
<DataGridTextColumn SortDirection="Descending" Width="301" Header="Ingredient" Binding="{Binding Ingredient}" IsReadOnly="True" CanUserSort="True" CanUserReorder="False" />
</DataGrid.Columns>
</DataGrid>
Editable column is optional. Preference.
Collection:
private ObservableCollection<RAM_Ingredient> MemberIngredientPrefs = new ObservableCollection<RAM_Ingredient>();
Binding:
dgIngredients.DataContext = MemberIngredientPrefs.OrderBy("Ingredient",true);
RAM_Ingredient:
public class RAM_Ingredient : INotifyPropertyChanged
and etc.
Where RAM_Ingredient.Preference:
private int _Preference;
public int Preference
{
get
{
return _Preference;
}
set
{
if ((_Preference != value))
{
SendPropertyChanging();
_Preference = value;
SendPropertyChanged("Preference");
}
}
}
The exception is:
System.InvalidOperationException was unhandled
Message='EditItem' is not allowed for this view.
Source=PresentationFramework
StackTrace:
at System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item)
at System.Windows.Controls.DataGrid.EditRowItem(Object rowItem)
at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e)
etc...