1

I have a Datagrid binded to an ObservabelCollection. The datagrid refresh very well when the changes are Add, remove ítems on the collection, but if i change a property of one Ítem on that collection, the grid doesn't refresh.

Grid definition:

        <DataGrid x:Name="DatGridPlanillas" ItemsSource="{Binding ListaPlanillas,Mode=TwoWay}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="{x:Static resources:Labels.GENERAL_IdDocumeto}" Binding="{Binding StrIdDocumento,Mode=TwoWay}" ClipboardContentBinding="{x:Null}"/>
                <DataGridTextColumn Header="{x:Static resources:Labels.GENERAL_FechaCreacion}" Binding="{Binding DatFechaDocumento}" ClipboardContentBinding="{x:Null}"/>
            </DataGrid.Columns>
        </DataGrid>

Changes

DocumentsBO MiGestorDeDocumentos = new DocumentsBO(db);
foreach (MyEntity Doc in ListaPlanillas)
{
    Documento DocumentoFinal = MiGestorDeDocumentos.NewDocIdByModule(_IntIdModulo);
    Doc.StrIdDocumento = DocumentoFinal.StrIdDocumento;
    Doc.IntIdDocumento = DocumentoFinal.IntIdDocumento;
    Doc.PlanillaAcopioGenerada = true;
    Doc.NumDocumentonumero = DocumentoFinal.NumDocumento;
    db.entry(Doc).State = EntityState.Modified;
}

The only way I found is to empty original Collection and then restore it

db.SaveChanges();
ObservableCollection<MyEntity> _tmp = _ListaPlanillas;
ListaPlanillas = new ObservableCollection<MyEntity>();
ListaPlanillas = _tmp; 

But this sounds to me very ugly way to perform something so simple. How can i do to force the grid to update when just a property of collection was changed?

Juan Pablo Gomez
  • 5,203
  • 11
  • 55
  • 101
  • What's the necessity to do changes in the way (I think) you are doing? Get the list from the db, wrap it in an observable collection, then you are done. That's how it normally works. – jamesSampica Oct 04 '13 at 13:43
  • I made some changes to the list programatically, just in some fields of each row, but the UI doesn't refresh that way. – Juan Pablo Gomez Oct 04 '13 at 15:48
  • The best way is as posted, implement `INotifyPropertyChanged` on your models. What you want to do is create partial classes in the same namespace of your entities models then write a notify handler in the setters. Rob didn't give you a good direction for that but [this question does that](http://stackoverflow.com/questions/11262816/partial-class-entity-framework-propertychanged). Yeah it will be a bit of boilerplate to set that up, but the good thing is that it will apply anywhere you use that model for databinding. – jamesSampica Oct 04 '13 at 16:33
  • tks @Shoe I'll take a look . – Juan Pablo Gomez Oct 04 '13 at 16:42

1 Answers1

1

You need to implement the INotifyPropertyChanged interface on the object that is being placed into the collection. Then fire PropertyChanged events when the object properties are updated. See http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx for an example.

Rob Goodwin
  • 2,676
  • 2
  • 27
  • 47
  • All my classes was generated using EF. It mean I must change manually this classes to implement INotifyPropertyChanged? – Juan Pablo Gomez Oct 03 '13 at 22:28
  • Sounds like this question then. http://stackoverflow.com/questions/14132349/implement-inotifypropertychanged-on-generated-entity-framework-classes – Rob Goodwin Oct 03 '13 at 22:32
  • It is a lot of work and overload for implement INotify.... on each property of my model. thats really a very extrange thing something so simple gets something complex. – Juan Pablo Gomez Oct 03 '13 at 22:38