[英]WPF datagrid "EditItem is not allowed for this view" exception
我以编程方式添加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);
当我添加项目时:
Globals_Liker.list_datagrid[tabControl1.SelectedIndex].Items.Add(Globals_Liker.list_item[tabControl1.SelectedIndex][i]);
但是如果我双击 Items 我有错误:
此视图不允许使用“EditItem”。
如何使该错误不弹出?
您should not update the Items directly of your DataGrid
而should not update the Items directly of your DataGrid
ItemsSource
设置为集合。 DataGrid 将从实现IEditableCollectionView
接口的 itemsource 中生成视图以允许编辑。 这个接口有函数EditItems()
可以让编辑发生。
所以为了解决这个问题。 在您的虚拟机/代码后面创建ObservableCollection
属性并将 DataGrid ItemsSource 设置为它,例如
ObservableCollection<Type> MyCollection{get;set;}
Globals_Liker.list_datagrid[tabControl1.SelectedIndex].ItemsSource = MyCollection;
在你的构造函数中,你可以通过 new 来初始化这个集合。 每当您想在DataGrid
添加项目时,只需将项目添加到 Observable 集合 (MyCollection) 中,它就会显示在网格上并且是可编辑的。
我在 3 种情况下看到此错误
案例1 :如果双击数据网格则显示此错误(包含分析等处理数据的自定义数据网格)
简单地,在 Datagrid 中设置 IsReadOnly="True"
case2 :编辑数据网格后显示此错误,必须在 RowEditEnding 期间设置
(sender as DataGrid).CommitEdit(DataGridEditingUnit.Row);
case3 :在 RowEditEnding 事件之后显示此错误,然后必须查看数据网格在哪里重新加载数据,如果视图源或数据网格已在使用中并且我们尝试手动覆盖数据,则可能会发生这种情况
如果您看到任何新病例,请告诉我
这对我有用
InitializeComponent();
datagrid.BeginningEdit += (s, ss) => ss.Cancel = true;
我尝试使用 IsReadOnly="True",但它不允许我检查和取消选中数据网格上的复选框
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.