Hi, see code below (Using EF5).
When an item is added or removed from the datagrid the CollectionChanged event is fired and can be handled, so an IsDirty flag can be set manually. But when changing an item, the changes are not catched by the CollectionChanged event.
Question: how to check if the collection has changes and if there is a need for calling SaveChanges(). I tried to implement the PropertyChanged event on the Observable Landen collection but I couldn't get this working.
<DataGridName="dgLand"ItemsSource="{Binding}"AutoGenerateColumns="True"HorizontalAlignment="Left"Margin="36,41,0,0"VerticalAlignment="Top"RenderTransformOrigin="-2.417,-2.308"Height="187"Width="364">
In code:
private static CollectionManagerEntities context;
private ObservableCollection<tblLand> Landen;
private ICollectionView DefaultLandenView;
private void DoBinding()
{
Landen = new ObservableCollection<tblLand>(context.tblLand);
DefaultLandenView = CollectionViewSource.GetDefaultView(Landen);
//CollectionChanged event
Landen.CollectionChanged += Landen_CollectionChanged;
//Binding the datagrid
Binding myBinding_Landen = new Binding();
myBinding_Landen.Source = Landen;
dgLand.SetBinding(DataGrid.ItemsSourceProperty, myBinding_Landen);
}
void Landen_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (tblLand myLand in e.NewItems)
{
context.tblLand.Add(myLand);
}
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (tblLand myLand in e.OldItems)
{
context.tblLand.Remove(myLand);
}
}
}
Regards Coen.