当绑定到WPF DataGrid时,此视图不允许使用Wrapped ObservableCollection <t>抛出`'EditItem'</t>

时间:2011-06-11 20:08:33

标签: c# wpf wpfdatagrid observablecollection

我创建了一个包装器来扩展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
}

在我尝试将列表中的项目绑定到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>

项目显示在网格中,但双击一个单元格会抛出'EditItem' is not allowed for this view.

当我用常规ModelCollection<TModel>替换ObservableCollection<T>时,不会抛出异常。

我的目的是允许对单元格进行编辑。我在包装器上错过了一个界面吗?

1 个答案:

答案 0 :(得分:4)

我能够通过明确实施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
}