简体   繁体   中英

Load ObservableCollection from Xml save file to Datagrid (C#, Wpf)

Okay so I hope it's okay if I don't post my code since it contains private parts.

I will describe it as good as I can. So I did an observablecollection without onpropetychange and a databind to the grid . Everything works well if I'm adding something to the collection, the datagrid updates. But if I get the observablecollection from the xml save file, it doesn't update.

So far I checked if the observablecollection loads everything (it does) and tried to update the datagrid manual (nothing). I'm glad if someone could give me advice without seeing the code. :)

My best guess is that you should set the DataGrid's ItemsSource property again after loading the ObservableCollection.

Use this:

datagrid.ItemsSource = null;
datagrid.ItemsSource = yourObservableCollection;

If you are changing the whole collection, the UI can not recognize the change since you are not using the INotifyPropertyChanged . You have two options:

  1. Implement the INotifyPropertyChanged and raise the event after setting a new collection as source.
  2. Clear the old collection, and fill in the new items to the old collection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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