简体   繁体   English

尝试编辑DataGrid单元格时发生InvalidOperationException

[英]InvalidOperationException when trying to edit a DataGrid cell

My purpose is to be able to bind to WPF controls classes that are obtained from some notification mechanism. 我的目的是能够绑定到从某些通知机制获得的WPF控件类。

I have defined an MyList class that holds MyItem objects. 我定义了一个MyList类,其中包含MyItem对象。 These MyItem objects have runtime defined properties. 这些MyItem对象具有运行时定义的属性。 So, MyItem implements ICustomTypeDescriptor. 因此,MyItem实现了ICustomTypeDescriptor。 I want the controls to be notified when values change, so it also implements INotifyPropertyChanged. 我希望控件在值更改时得到通知,因此它还实现了INotifyPropertyChanged。 And because I had problem with edition, I also made it implement IEditableObject. 并且因为我在版本方面遇到问题,所以我也使它实现了IEditableObject。

So, I have the MyItem class defined as: 因此,我将MyItem类定义为:

public class MyItem: BaseClass, INotifyPropertyChanged, ICustomTypeDescriptor, IEditable {
//...
}

where BaseClass is my original class I want to make visible to WPF controls. 我想使BaseClass是我的原始类对WPF控件可见。

MyList, on its side, implements IEnumerable as this is what is expected for DataGrid.ItemsSource, INotifyCollectionChanged because I want the control to be notified for new rows, IEditableCollectionView and IList to try to solve my edition problem. MyList方面实现了IEnumerable,因为这是DataGrid.ItemsSource,INotifyCollectionChanged的期望值,因为我希望控件收到新行的通知,IEditableCollectionView和IList试图解决我的版本问题。

So, I have the following class definition: 因此,我有以下类定义:

public class MyList : BaseClass, INotifyCollectionChanged, IEnumerable<MyItem>,
                      IEditableCollectionView, IList<MyItem>
{
  private List<MyItem> _list = new List<MyItem(); // holds items
//...
}

When I set this list as items source to a data grid, everything seems to work fine: The column of the data grid are the declared dynamic properties through the ICustomTypeDescriptor interface. 当我将此列表设置为数据网格的项目源时,一切似乎工作正常:数据网格的列是通过ICustomTypeDescriptor接口声明的动态属性。 The value of each cell is correctly displayed. 正确显示每个单元格的值。

But I soon as I double click on a cell for editing it, I get an InvalidOperationException without any stack trace or more explicit message. 但是,我双击单元格进行编辑后,立即收到InvalidOperationException,而没有任何堆栈跟踪或更明确的消息。

I have no idea on what I should do to make it work. 我不知道我应该怎么做才能使其工作。 I have tried all the possible solutions I have found here or elsewhere on the web. 我已经尝试了在这里或其他任何地方找到的所有可能的解决方案。

Has anyone an idea on what is going wrong? 有谁知道出什么问题了吗?

尝试在MyList类中实现非通用IList接口,并确保您没有将任何项目直接添加到DataGrid控件的Items集合中: https : //social.msdn.microsoft.com/Forums/en -US / 9deaa308-37e8-4110-814f-30d2de7c2654 /数据网格-edititem-IS-不被允许换这个视图?论坛= WPF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM