2013-10-03 32 views
1

我有一個綁定到ObservabelCollection的Datagrid。當更改是添加,刪除集合上的項目時,DataGrid刷新得非常好,但是如果我更改了該集合上的一個項目的屬性,則網格不會刷新。WPF datagrid在單元級別發生更改時刷新

電網定義:

 <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> 

變化

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; 
} 

我發現的唯一辦法是空的原始集合,然後將其還原

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

但是這對我聽起來很醜陋方式來執行如此簡單的事情。我怎麼能強制網格更新時,只是一個集合的屬性被改變?

+0

有什麼需要改變的方式(我認爲)你在做什麼?從數據庫中獲取列表,將其包裝在可觀察的集合中,然後完成。這就是它通常的工作原理。 – Shoe

+0

我以編程方式對列表進行了一些更改,只是在每行的某些字段中進行了更改,但UI不會以此方式刷新。 –

+0

最好的辦法就像發佈一樣,在你的模型上實現'INotifyPropertyChanged'。你想要做的是在實體模型的相同名稱空間中創建部分類,然後在設置器中編寫通知處理程序。羅布沒有給你一個很好的方向,但[這個問題呢](http://stackoverflow.com/questions/11262816/partial-class-entity-framework-propertychanged)。是的,這將會是一個樣板,但最好的一點是,它將適用於您使用該模型進行數據綁定的任何地方。 – Shoe

回答

0

您需要在要放入集合的對象上實現INotifyPropertyChanged接口。然後在對象屬性更新時觸發PropertyChanged事件。例如,請參閱http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

+0

我所有的課程都是使用EF生成的。這意味着我必須手動更改這些類來實現INotifyPropertyChanged? –

+0

聽起來像這個問題。 http://stackoverflow.com/questions/14132349/implement-inotifypropertychanged-on-generated-entity-framework-classes –

+0

對於我的模型的每個屬性,實現INotify ....都有很多工作和過載。那真的是一件非常非常容易的事情,那麼簡單的事情會變得複雜。 –