I have a silverlight app using RIA DataDomainService.
Silverlight app has one page with DataGrid.
I’ve set the ItemSource property of the DataGrid to the list in the Loaded event e.g.
//gets the list
List<Data> data = GetTheList();//returns the list of data
dataGrid.ItemSource = data;
This works the first time. The second time, I use the same above lines but I insert a new Data object in the list and then bind the list to the dataGrid using
dataGrid.ItemSource = data but it does not update the grid.
Grid remains the same.
On the xaml side, in the DataGrid tag:
ItemSouce = {Binding data, Mode=TwoWay}
Is this binding correct? Why does it bind the first time and not the second time with the new list?
First off, setting the ItemSource in both XAML and code is redundant — the code behind will overwrite your XAML Binding setting.
Try using an ObservableCollection instead of a List – it automatically notifies the View when items are added or removed. Then you shouldn’t have to set data.ItemSource more than once.
When you add or remove items from the ObservableCollection, the grid should automatically change. You’ll have to make sure GetTheList() returns an ObservableCollection, and use that to store your ‘Data’ objects.
* edit – if using ObservableCollection doesn’t fit well with existing code, try setting ItemsSource to null before updating it. ex: