WPF datagrid“该视图不允许”EditItem“例外 [英] WPF datagrid "EditItem is not allowed for this view" exception

查看:2627
本文介绍了WPF datagrid“该视图不允许”EditItem“例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式添加 DataGrid

  System.Windows.Controls .DataGrid dataGrid = new System.Windows.Controls.DataGrid(); 
dataGrid.GridLinesVisibility = DataGridGridLinesVisibility.None;
dataGrid.Horizo​​ntalScrollBarVisibility = 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);

添加项目时:


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


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


此视图不允许EditItem。


如何使该错误不会弹出?

解决方案

不应直接更新DataGrid的项目,而应将 ItemsSource 设置为收藏。 DataGrid将从实现 IEditableCollectionView 接口的itemsource生成视图,以便允许编辑。该界面具有函数 EditItems(),让编辑发生。



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

 code> ObservableCollection< Type> MyCollection {get; set;} 


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

在您的构造函数中,您可以通过新建它来初始化此集合。每当你想在 DataGrid 中添加项目时,只需在Observable集合(MyCollection)中添加该项目,它将显示在网格上,并且可以编辑。


I programmatically add 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]);

But if I doubleclick on Items I have error:

"EditItem" is not allowed for this view.

How to make that error does not pop up?

解决方案

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

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

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.

这篇关于WPF datagrid“该视图不允许”EditItem“例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆