I have a DataGrid in WPF (a class that extends DataGrid), and I would like to edit the items in it. But of course I am getting the following error:
我在WPF中有一个DataGrid(一个扩展DataGrid的类),我想编辑它中的项目。但是我当然得到以下错误:
Operation is not valid while ItemsSource is in use.
Access and modify elements with ItemsControl.ItemsSource instead.
I have tried changing the itemsSource of the DataGrid, and then adding the items, but I still get the same error. Something like:
我已经尝试更改DataGrid的itemsSource,然后添加项目,但我仍然得到相同的错误。就像是:
public class MyDG:DataGrid{
public void add(){
List<TimesheetRecord> records = new List<TimesheetRecord>();
foreach(TimesheetRecord rec in this.Items){
records.Add(rec);
}
//DO SOME STUFF, ADD MORE ITEMS TO records
ItemCollection col = this.Items;
this.ItemsSource = records;
col.Clear();
foreach(TimesheetRecord rec in records){
col.add(red);//exception thrown here
}
this.ItemsSource = col;
}
}
I don't understand why I am getting that error, when I have already changed the itemsSource to a different list...?
当我已经将itemsSource更改为其他列表时,我不明白为什么我会收到该错误?
I can't (easily) add the items to the list which is originally bound as the itemsSource, because that list exists in a different class. Would it be best for me to just have a global variable in the MyDG class that is List<TimesheetRecord> myItems = new List<TimesheetRecord>();
and then in the constructor for MyDG go this.ItemsSource = myItems
我不能(轻松地)将项目添加到最初绑定为itemsSource的列表中,因为该列表存在于不同的类中。我最好在MyDG类中有一个全局变量,即List
Or do you have any other suggestions how I should go about doing this? I am open to anything, as this is the first time I have used databinding, so I am probably doing something wrong...
或者你有什么其他的建议我应该怎么做呢?我对任何事情都持开放态度,因为这是我第一次使用数据绑定,所以我可能做错了...
3 个解决方案
#1
4
Decalre records collection as:
Decalre将收集记录为:
ObservableCollection<TimesheetRecord> records = new ObservableCollection<TimesheetRecord>();
and keep it data-bound to the DataGrid. Manipulate records collection as needed, data binding will take care of keeping UI in sync with the collection.
并将其数据绑定到DataGrid。根据需要处理记录集合,数据绑定将保持UI与集合同步。
#2
2
You have to choose whether to use Items or ItemsSource, you can't use both interchangably. Attempting to modify Items while using ItemsSource assumes an implicit conversion that isn't supported, hence the error.
您必须选择是否使用Items或ItemsSource,您不能同时使用两者。尝试在使用ItemsSource时修改项目假定不支持隐式转换,因此错误。
In this case, it seems like the best approach might be to just set Items and add to that collection directly. To use ItemsSource, you'd need to, exactly as you wrote, pass a reference to the ItemsSource collection (List<TimesheetRecord>) in to your DataGrid class.
在这种情况下,似乎最好的方法可能是设置Items并直接添加到该集合。要使用ItemsSource,您需要完全按照您的编写方式将对ItemsSource集合(List
#3
1
Once you assign "records" to the ItemsSource, you've already updated your collection. There's no need to manually add items to the dataGrid.Items collection.
将“记录”分配给ItemsSource后,您已经更新了集合。无需手动将项添加到dataGrid.Items集合中。
#1
4
Decalre records collection as:
Decalre将收集记录为:
ObservableCollection<TimesheetRecord> records = new ObservableCollection<TimesheetRecord>();
and keep it data-bound to the DataGrid. Manipulate records collection as needed, data binding will take care of keeping UI in sync with the collection.
并将其数据绑定到DataGrid。根据需要处理记录集合,数据绑定将保持UI与集合同步。
#2
2
You have to choose whether to use Items or ItemsSource, you can't use both interchangably. Attempting to modify Items while using ItemsSource assumes an implicit conversion that isn't supported, hence the error.
您必须选择是否使用Items或ItemsSource,您不能同时使用两者。尝试在使用ItemsSource时修改项目假定不支持隐式转换,因此错误。
In this case, it seems like the best approach might be to just set Items and add to that collection directly. To use ItemsSource, you'd need to, exactly as you wrote, pass a reference to the ItemsSource collection (List<TimesheetRecord>) in to your DataGrid class.
在这种情况下,似乎最好的方法可能是设置Items并直接添加到该集合。要使用ItemsSource,您需要完全按照您的编写方式将对ItemsSource集合(List
#3
1
Once you assign "records" to the ItemsSource, you've already updated your collection. There's no need to manually add items to the dataGrid.Items collection.
将“记录”分配给ItemsSource后,您已经更新了集合。无需手动将项添加到dataGrid.Items集合中。