Adding an item to the <CustomClass> list, which is a DataBinded for a DataGrid

Basically I am trying to add a new item to a DataGrid that has a DataBinded List <>. I already tried everything that I could think of, but I always get the exception "Faulty ItemsControl element with its source source." If someone helped me with this, I would be supportive

-2
source share
2 answers

Are you adding items to the List <CustomClass> data list in the background of the stream? You can then use the dispatcher to sort the Add call to return the UI thread:

Application.Current.Dispatcher.BeginInvoke(new Action(()=> { yourCollection.Add(yourItem); }))); 

Do this for all add and delete operations that modify the original collection.

You should also replace List <CustomClass> with ObservableCollection <gt; if you want the DataGrid to automatically update as you add items to the original collection. ObservableCollection provides notification of changes, but is not listed: http://msdn.microsoft.com/en-us/library/ms668604%28v=vs.110%29.aspx .

Also, make sure that you add items to the source collection, and not to the Items property of the DataGrid control.

+1
source

Try using the EnableCollectionSynchronization method on the ItemsSource .

0
source

Source: https://habr.com/ru/post/1265174/


All Articles