KillemAll
KillemAll

Reputation: 701

ICollection.Add(item) don´t raise propertychanged MVVM datagrid

My problem is this: I use EF code first and I got a master detail situation, the master have a childs wich are the itemsSource for a datagrid, like this:

<DataGrid ItemsSource="{Binding Path=Transaction.StockItems,Mode=TwoWay}" 
          SelectedItem="{Binding Path=StockItem, Mode=TwoWay}" 
          AutoGenerateColumns="False" IsReadOnly="True">
    <DataGrid.Columns>
        <mui:DataGridTextColumn Width="0.12*" Binding="{Binding Name}"/>
        <mui:DataGridTextColumn Width="0.12*" Binding="{Binding Cod}"/>
        <mui:DataGridTextColumn Width="0.12*" Binding="{Binding Cost}"/>
        <mui:DataGridTextColumn Width="0.15*" Binding="{Binding Qt}"/>
    </DataGrid.Columns>
</DataGrid>

In my ViewModel I got a property for the Transaction property, like this:

    private tTran_t;
    public tTransaccion Transaction
    {
        get { return _t; }
        set { _t= value; RaisePropertyChanged("Transaction"); }
    }

In other method of my ViewModel I do an Add() to the collection, like this:

    Transaction.StockItems.Add(myNewLine);
    RaisePropertyChanged("Transaction");

In my Model the definition of Transaction.StockItems is ICollection.

The problem: the datagrid never got updated

I guess this is because the RaisPropertyChanged() is never called, because the collection is never set, but as you can see, I do call the RaisPropertyChanged() of the Transaction object.

Thanks in advance

Upvotes: 0

Views: 170

Answers (1)

Franck Ngako
Franck Ngako

Reputation: 157

adding an item to a collection doesn't "change" the object itself. a RaisepropertyChanged is raised when the collection itself is set. so what you need to do is assigning a new collection to your collection reference. So First you add item to a temporary collection then you do transaction = new collection(temporaryCollection) then your UI will ne notified of a change.

Upvotes: 1

Related Questions