Why does the DataGrid not update when the ItemsSource is changed?

白昼怎懂夜的黑 提交于 2019-11-28 18:38:26
H.B.

The ItemsSource is always the same, a reference to your collection, no change, no update. You could null it out before:

dgOrderDetail.ItemsSource = null;
dgOrderDetail.ItemsSource = OrderDetailObjects;

Alternatively you could also just refresh the Items:

dgOrderDetail.ItemsSource = OrderDetailObjects; //Preferably do this somewhere else, not in the add method.
dgOrderDetail.Items.Refresh();

I do not think you actually want to call UpdateLayout there...

(Refusing to use an ObservableCollection is not quite a good idea)

I also found that just doing

dgOrderDetails.Items.Refresh();

would also accomplish the same behavior.

If you bind the ItemSource to a filtered list with for example Lambda its not updated. Use ICollectionView to solve this problem (Comment dont work):

//WindowMain.tvTemplateSolutions.ItemsSource = this.Context.Solutions.Local.Where(obj=>obj.IsTemplate); // templates
ICollectionView viewTemplateSolution = CollectionViewSource.GetDefaultView(this.Context.Solutions.Local);
viewTemplateSolution.SortDescriptions.Clear();
viewTemplateSolution.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
viewTemplateSolution.Filter = obj =>
{
   Solution solution = (Solution) obj;
   return solution.IsTemplate;
};
WindowMain.tvTemplateSolutions.ItemsSource = viewTemplateSolution;

i use ObservableCollection as my items collection and than in the view model call CollectionViewSource.GetDefaultView(my_collection).Refresh();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!