DataGrid 在 CompositeCollection 上崩溃编辑
我有一个数据网格。 它的 ItemsSource 通过 ViewModel 绑定到 ModelView 的 CompositeCollection。 CompositeCollection 由 2 个 ObservableCollections 组成。
网格上的显示效果很好。我能够看到该集合。 但是,当我尝试编辑其中一行时,出现以下崩溃(NotSupportedException): “此视图不允许‘EditItem’”
如何使行可编辑?我需要能够编辑代表网格中每一行的模型视图。
以下是我用来绑定到 itemssource 的 CompositeCollection 属性代码: 这不是确切的代码,因为我不允许发布确切的代码,但它与我如何将集合设为
public CompositeCollection ModelViewsCollection 的逻辑相同 { 得到 { CollectionContainer模型ViewContainer;
CompositeCollection modelViewCollection = new CompositeCollection();
modelViewContainer= new CollectionContainer();
modelViewContainer.Collection= this.ModelViewCollection;
modelViewCollection .Add(modelViewContainer);
modelViewContainer=新的CollectionContainer(); modelViewContainer.Collection= this.ModelViewCollection2; modelViewCollection .Add(modelViewContainer);
return modelViewCollection;
}
}
I have a DataGrid.
It's ItemsSource is bound to the ModelView's CompositeCollection through the ViewModel.
The CompositeCollection consists of 2 ObservableCollections.
The display on the grid is fine. I am able to see the collection.
However, when I try to edit one of the rows, I get a crash (NotSupportedException) of:
"'EditItem' is not allowed for this view"
How do I make the rows editable? I need to be able to edit the ModelViews representing each row in the Grid.
Here is the CompositeCollection Property code that I use to bind to the itemssource:
this isn't the exact code since I am not allowed to post the exact code but it is the same logic on how I make the collection
public CompositeCollection ModelViewsCollection
{
get
{
CollectionContainer modelViewContainer;
CompositeCollection modelViewCollection = new CompositeCollection();
modelViewContainer= new CollectionContainer();
modelViewContainer.Collection= this.ModelViewCollection;
modelViewCollection .Add(modelViewContainer);
modelViewContainer= new CollectionContainer();
modelViewContainer.Collection= this.ModelViewCollection2;
modelViewCollection .Add(modelViewContainer);
return modelViewCollection;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CompositeCollection 未实现数据网格用于编辑的 IEditableCollectionView。
我遇到了同样的问题,最终在视图模型上做了我自己的 fake 复合集合,类似于你所拥有的,如果你放入集合中的只是两个可观察的集合,那么它不是很难跟踪听听他们两个上的收藏变化的变化。并让你的 viewmodels 集合包含这两个集合
你甚至可以像我一样进行肮脏的黑客攻击,重建网格绑定到的 ObservableCollection ,每次其中一个集合发生变化时(我知道这并不优雅,但我会在什么时候返回并优化)我有时间..即从来没有)通过 linq 查询,这个东西真的很容易。
否则,也许您可以从 CompositeCollection 派生并尝试添加 IEditableCollectionView,如果您成功,请务必让我知道。
这是数据网格论坛上的相同的问题
CompositeCollection does not implement IEditableCollectionView which is used by the datagrid to edit.
I have had the same issues, and ended up doing my own fake composite collection on the view model, similiar to what you have, if all you are putting in your collection is two observable collections, its not to hard to track the changes listening to collection changed on both of them. and make your viewmodels collection consist of both of them
You could even do the dirty hack that i did, of rebuilding the ObservableCollection that the grid binds to every time one of the collections change (not elegant i know, but ill go back and optimise when i get time.. i.e. never) With a linq query this stuff is really easy.
Otherwise maybe you could derive from CompositeCollection and try and add the IEditableCollectionView, if you get that working be sure to let me know.
here is the same question on the datagrid forum