简体   繁体   English

WPF DataGrid编辑难度(InvalidOperationException)

[英]WPF DataGrid Editing Difficulty (InvalidOperationException)

I have a DataGrid that I am filling with information from an ObservableCollection<ObservableCollection<T>> where T is an object of mine that implements IEditableObject . 我有一个DataGrid ,我在其中填充了ObservableCollection<ObservableCollection<T>> ,其中T是我的实现IEditableObject的对象。

I have the DataGrid correctly filling with the information I need. 我让DataGrid正确填充了我需要的信息。 The problem is that when I go to edit a box in it I get an exception. 问题是,当我去编辑其中的一个框时,出现异常。

System.InvalidOperationException: 'EditItem' is not allowed for this view

followed by the subsequent stack trace. 随后是后续的堆栈跟踪。

I understand binding as well as the idea that in an ObservableCollection<T> if you want to be able to edit the DataGrid items than you need to implement the IEditableObject interface in the object T . 我了解绑定以及在ObservableCollection<T>的想法,如果您希望能够编辑DataGrid项,而不需要在对象T实现IEditableObject接口。

I'm not sure why it won't let me edit, although I'm leaning more towards the idea that I need to attach my object to some type of view. 我不确定为什么它不允许我进行编辑,尽管我更倾向于需要将对象附加到某种视图上的想法。

I've not found much on what I'm trying to do but I have been fairly successful up until this point in getting over hurdles. 我在尝试做的事情上并没有发现太多,但是到目前为止,我在克服障碍方面一直相当成功。 If anyone has any insight as to what I should do it is much appreciated. 如果有人对我应该做什么有任何见解,将不胜感激。

Also, I will post code if someone would seriously like to see it but I believe it's less about what I've already written and more about what I still need to write. 另外,如果有人真的想看一下代码,我会发布代码,但是我相信这与我已经写的东西少有关,而与我仍然需要写的东西有关。

Edit: Added code 编辑:添加代码

private void dg_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        DataGrid grid = sender as DataGrid;
        grid.Columns.Clear();
        TableNode node = e.NewValue as TableNode;
        if (node != null)
        {
            InfoTable dti = new InfoTable(m_model.EditNode.Database.Path, node.FullName);
            int index = 0;
            foreach (string colName in dti.Columns)
            {
                DataGridTextColumn col = new DataGridTextColumn();
                col.Header = colName;
                col.Binding = new Binding("[" + index + "].Info");
                //Note: .Info is the information string in the class T containing the data that may or may not be edited
                grid.Columns.Add(col);
                index++;
            }
            grid.ItemsSource = dti;
        }
    }

Here I am binding the columns to each ObservableCollection in the ObservableCollection<ObservableCollections<T>> that is in my InfoTable . 在这里,我将列绑定到InfoTable ObservableCollection<ObservableCollections<T>>中的每个ObservableCollection I have implemented IEnumerable and INotfiyPropertyChange in the InfoTable . 我已经在INotfiyPropertyChange中实现了IEnumerableInfoTable

I'm pretty sure your InfoTable class needs to implement IList , too, in order to support DataGrid editing. 我很确定您的InfoTable类也需要实现IList ,以支持DataGrid编辑。

IList adds the capability of adding and removing items, and the DataGrid probably needs that to manage its rows. IList增加了添加和删除项的功能,DataGrid可能需要用它来管理其行。

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

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