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

前端 未结 4 1750
野趣味
野趣味 2020-12-05 02:18

I have a datagrid in my wpf application and I have a simple problem. I have a generic list and I want to bind this collection to my datagrid data source every time an object

相关标签:
4条回答
  • 2020-12-05 02:51

    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;
    
    0 讨论(0)
  • 2020-12-05 02:52

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

    0 讨论(0)
  • 2020-12-05 02:57

    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)

    0 讨论(0)
  • 2020-12-05 03:00

    I also found that just doing

    dgOrderDetails.Items.Refresh();
    

    would also accomplish the same behavior.

    0 讨论(0)
提交回复
热议问题