WPF 数据网格“此视图不允许编辑项”异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19334326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:50:11  来源:igfitidea点击:

WPF datagrid "EditItem is not allowed for this view" exception

c#.netwpfdatagrid

提问by prm

I programmatically add DataGrid:

我以编程方式添加DataGrid

System.Windows.Controls.DataGrid dataGrid = new System.Windows.Controls.DataGrid();
dataGrid.GridLinesVisibility = DataGridGridLinesVisibility.None;
dataGrid.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
dataGrid.Background = Brushes.White;
DataGridTextColumn textColumn = new DataGridTextColumn();
textColumn.Width = 250;
textColumn.Header = "Account";
textColumn.Binding = new Binding("Account");
dataGrid.Columns.Add(textColumn);

When I add Item:

当我添加项目时:

Globals_Liker.list_datagrid[tabControl1.SelectedIndex].Items.Add(Globals_Liker.list_item[tabControl1.SelectedIndex][i]);

Globals_Liker.list_datagrid[tabControl1.SelectedIndex].Items.Add(Globals_Liker.list_item[tabControl1.SelectedIndex][i]);

But if I doubleclick on Items I have error:

但是如果我双击项目我有错误:

"EditItem" is not allowed for this view.

此视图不允许使用“EditItem”。

How to make that error does not pop up?

如何使该错误不弹出?

回答by Nitin

You should not update the Items directly of your DataGridbut rather set the ItemsSourceto the collection. DataGrid will generate the view out of the itemsource that implements IEditableCollectionViewinterface in order to allow the editing. This interface has function EditItems()which let the editing happen.

should not update the Items directly of your DataGrid而是将 设置ItemsSource为集合。DataGrid 将从实现IEditableCollectionView接口的项目源中生成视图以允许编辑。该界面具有EditItems()让编辑发生的功能。

So in order solve this problem. Create the ObservableCollectionproperty in your VM/Code behind and set the DataGrid ItemsSource to it like

所以为了解决这个问题。ObservableCollection在后面的 VM/代码中创建属性并将 DataGrid ItemsSource 设置为

ObservableCollection<Type> MyCollection{get;set;}


Globals_Liker.list_datagrid[tabControl1.SelectedIndex].ItemsSource = MyCollection;

In your constructor you can initialize this collection by newing it. And whenever you want to add item in your DataGrid, just add the item in the Observable collection (MyCollection), it will be shown on grid and will be editable.

在你的构造函数中,你可以通过 new 来初始化这个集合。每当您想在您的DataGrid.

回答by Prashant Manjule

I seen this error in 3 cases

我在 3 种情况下看到此错误

case1: this error shown if double clicking the datagrid then(custom datagrid which contains processed data like analysis)

案例1:如果双击数据网格则显示此错误(自定义数据网格包含分析等处理过的数据)

Simply, Set in Datagrid IsReadOnly="True"

简单地,在 Datagrid 中设置 IsReadOnly="True"

case2: this error shown after editing the datagrid, must set during RowEditEnding

case2:编辑数据网格后显示此错误,必须在 RowEditEnding 期间设置

  (sender as DataGrid).CommitEdit(DataGridEditingUnit.Row);

case3: this error shown after RowEditEnding event, then must see where the datagrid is reloading the data, it can happen if viewsource or datagrid is already in use and we try to override the data manually

case3:在 RowEditEnding 事件之后显示此错误,然后必须查看数据网格在哪里重新加载数据,如果视图源或数据网格已在使用中并且我们尝试手动覆盖数据,则可能会发生这种情况

Let me know if any new cases you seen

如果您看到任何新病例,请告诉我