1

I have this ViewModel:

public class CombustiblesViewModel
{        
    public List<CombustiblesWCFModel> Combustibles{ get; set; }        

    public CombustiblesViewModel()
    {
        Combustibles = _svc.Combustibles_List(sTicket);                            
    }      
}  

Combustibles_List Returns a List From a WCF Service. What I need is to Track changes on Each CombustiblesWCFModel Object. So I Extend my Model with this code:

public CombustiblesWCFModel()
    {
        this.PropertyChanged += ChangedRow;   
    }

    private void ChangedRow(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "HasChanges") return;
        CombustiblesWCFModel p = (CombustiblesWCFModel)sender;            
        // Should Rise HasChanges Property on model, but it doesn't work
        p.HasChanges = true;
    }

    private bool _haschanges;
    public bool HasChanges
    {
        get
        {
            return _haschanges;
        }
        set
        {
            _haschanges = value;
            this.RaisePropertyChanged("HasChanges");
        }
    }
}

My problem is that HasChanges is Always false. I believe that PropertyChanged event is overrited when the model is returned from WCF Service.

Question

So How can I detect Model changes on each object of the viewmodel collection?

ericpap
  • 2,917
  • 5
  • 33
  • 52

2 Answers2

0

WCF contracts are serialized objects, they should be viewed as immutable.

What you want to do is have a local cache where you merge changes into your stateful copy. This might be as simple as mapping data from the contract to your class that implements INotifyPropertyChanged properly. For simple mapping tools like AutoMapper go a long way.

For more complex merging and diffing, there is the Compare .NET Objects project that will provide fine grained information.

Other tools that might be of use are Reactive Extensions observable collections where you could insert the data contracts and subscribe to changes.

The other alternative is to actually move to a publisher-subscriber model with WCF. IDesign and Juval Lowy provide a ready to go implementation of Pub-Sub with WCF.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
0

If your collection is changing and you need to get the updated Collection you can go with using ObservableCollection in place of the List which automatically implements NotifyPropertyChanged event.