none
DataContext Events RRS feed

  • Question

  • I am new with WPF and LinqToSQL, but I am learning and these forums have been extremely valuable, thanks to all who contribute in helping guys like myself.
    I have started to write a WPF application using LinqToSQL and for the bindings I am using CollectionViews.

    I want to use DataContext.GetChangeSet() to give the user a display of how many inserts/updates/deletes has been made to the DataContext which has not yet been persisted to the database.

    My problem is that I see no event that is raised by the DataContext to determine if there has been any change to the cached data, so that I can call GetChangeSet(). It would have been nice to have some kind of "DataChanged" event that gets triggerred whenever there is an insert/update/delete. I cannot see events on the CollectionViews either that indicate a change.

    For now I am using the WPF Grid's "SelectionChanged" event to update the counter whenever the user navigates to a different row, but I am looking for a more elegant approach.

    I would appreciate it very much if someone can give me some direction.

    Kind regards
    Anton.

    Friday, June 5, 2009 7:43 AM

Answers

  • Try to override the Update methods using ExecuteDynamicUpdate to gain the full control over the changes. Here is the code sample:
    public partial class MyClass { 
      public MyClass() : base() { 
        OnCreated(); 
      } 
      partial void DeleteMyClass(MyClass instance) { 
        deletedCount++; 
        this.ExecuteDynamicUpdate(instance); 
      } 
      partial void UpdateMyClass(MyClass instance) {   
      updatedCount++; 
      this.ExecuteDynamicUpdate(instance); 
      } 
    }
    • Marked as answer by Anton Marais Saturday, June 6, 2009 2:20 AM
    Friday, June 5, 2009 11:17 AM

All replies

  • One very quick solution would be to hook the PropetyChanged event of the individual dataitems inside your collection. But beware that this could generate too many events since it fire on every change in every property of your data object.

    Another method would be to extend the partial classes that are generated by LINQ and raise a custom event by providing an implementation for the LINQ-generated partial methods.

    One more method would be to make your LINQ generated classes implement IEditableObject and raise a custom event in the EndEdit() method implementation.

    Hope that helps.

    Regards,
    Syed Mehroz Alam
    My Blog | My Articles
    Friday, June 5, 2009 10:30 AM
  • Try to override the Update methods using ExecuteDynamicUpdate to gain the full control over the changes. Here is the code sample:
    public partial class MyClass { 
      public MyClass() : base() { 
        OnCreated(); 
      } 
      partial void DeleteMyClass(MyClass instance) { 
        deletedCount++; 
        this.ExecuteDynamicUpdate(instance); 
      } 
      partial void UpdateMyClass(MyClass instance) {   
      updatedCount++; 
      this.ExecuteDynamicUpdate(instance); 
      } 
    }
    • Marked as answer by Anton Marais Saturday, June 6, 2009 2:20 AM
    Friday, June 5, 2009 11:17 AM