Home > Software engineering >  Binding DataGrid ItemSource to CollectionView does not work
Binding DataGrid ItemSource to CollectionView does not work

Time:01-25

I am working on a small WPF project and need to bind a CollectionView to a DataGrid. Therefore, I call the following command in a method to set the CollectionView's data:

 BlInvoicesCollectionView = (CollectionView)CollectionViewSource.GetDefaultView(BlInvoices);
BlInvoicesCollectionView.Filter = FilterBlInvoices;

BlInvoices is just a normal list with items of a model I created. When I debung the code, the CollectionView contains the values it should. However, the Items are not displayed in the DataGrid and I couldn't figure out why yet.

CodePudding user response:

If you have a List of type BlInvoices (List<BlInvoices>) called BlInvoicesCollection, then you can simply use:

dataGrid.ItemsSource = BlInvoicesCollection;

CodePudding user response:

Solved my issue by implementing a new property of type CollectionView, which is calls the PropertyChanged everytime it is set.

public CollectionView BlInvoicesCollectionView { get{ return blInvoicesCollectionView; } 
            set { blInvoicesCollectionView = value; OnPropertyChanged(nameof(BlInvoicesCollectionView)); } }

  •  Tags:  
  • Related