Wrapped ObservableCollection<T> throwing `'EditItem' is not allowed for this view` when bound to a WPF DataGrid

三世轮回 提交于 2019-12-24 17:26:46

问题


I created a wrapper to extend an ObservableCollection<T>

[Serializable]
public abstract class ModelCollection<TModel> : ModelCollectionBase,
    IList<TModel>, INotifyCollectionChanged, INotifyPropertyChanged
    where TModel : ModelBase<TModel>
{
    private ObservableCollection<TModel> wrappedCollection = new ObservableCollection<TModel>();

    // wrapper implementation goes here
}

I thought it was working fine until I attempted to bind items from a list to a DataGrid.

<DataGrid ItemsSource="{Binding /Orders}" AutoGenerateColumns="False">
    <DataGrid.Columns>
    <DataGridTextColumn Header="Order Id" Binding="{Binding OrderId}" />
        <DataGridTextColumn Header="Date Time" Width="125" Binding="{Binding DateTime}" />
        <DataGridTextColumn Header="Notes" Width="125" Binding="{Binding Notes}" />
        <DataGridTextColumn Header="Cost" Width="75" Binding="{Binding Cost}" />
    </DataGrid.Columns>
</DataGrid>

The items appear in the grid, but double clicking a cell throws 'EditItem' is not allowed for this view.

The exception isn't thrown when I replace my ModelCollection<TModel> with a regular ObservableCollection<T>.

My intent is to allow editing on the cells. Am I missing an interface on my wrapper?


回答1:


I was able to fix this by explicitly implementing IList

[Serializable]
public abstract class ModelCollection<TModel> : ModelCollectionBase,
    IList<TModel>, IList, INotifyCollectionChanged, INotifyPropertyChanged
    where TModel : ModelBase<TModel>
{
    private ObservableCollection<TModel> wrappedCollection = new ObservableCollection<TModel>();

    // wrapper implementation goes here
}


来源:https://stackoverflow.com/questions/6318276/wrapped-observablecollectiont-throwing-edititem-is-not-allowed-for-this-vie

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