问题
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