ericpap
ericpap

Reputation: 2937

How can i refresh my DataGrid on WPF

I have a problem.

I have this WPF GridView

<DataGrid 
  ItemsSource="{Binding Path=myOrder.Detail}">
</DataGrid>

My property on the viewModel looks like this:

public myOrderModel myOrder { get; set; }

and this is my model:

public partial class myOrderModel
{
    public int ID_Order { get; set; }
    public List<myOrdenDetail> Detail { get; set; }
}

On the constructor of the viewmodel y fill my Order with data from a WCF service and the detail is show on the grid correctly (of course this code is simplified). Now I need to modify de Detail collection (add, remove or modify) from inside the viewmodel. The problem is that the gridview is not refreshed to show this changes.

How can i notify the grid to reflect the changes inside Detail List? Thanks!!!!

EDIT

As suggested i try with ObservableCollection and it worked. In the viewModel I add a property:

public ObservableCollection<myOrdenDetail> Detail{ get; set; }

and Map the model to my new property:

Detail = new ObservableCollection<myOrdenDetail>(myOrder.Detail);

Of course i need to map Detail back to the original model when I save the information. I could also change the original model behind WCF service but i don't know how would WCF react with ObservableCollection.

Thanks!

Upvotes: 2

Views: 63

Answers (1)

viggity
viggity

Reputation: 15237

WPF needs to know that items have changed and List<T> does not implement INotifyCollectionChanged. If you were to change your Detail property to be ObservableCollection<myOrderDetail> then you should be good to go and the grid will update when items get added or removed.

Upvotes: 2

Related Questions