tags:

views:

25

answers:

2

Hi all, I have a datagrid that is bound to a observableCollection of Employees The user is allowed to do in line editing by double clicking the datagridRow. When binding the property in question I also use UpdateSourceTrigger.

When I user presses the save button ,the saveCommand is triggered in my MVVM and I want to create a list of only the employees that I have had property modified.

All my ViewModels implements INotifyPropertyChanged.

Despite lots of links on google I cannot seem to find an example that takes you through or explain how to track the items that have changed in a observablecollection.

Can you help?

+2  A: 

Create base class for your Employee, for example, EntityBase and enum describing its states:

    public enum EntityState
    {
        NotChanged,
        Changed,
        Added,
        Deleted
    }

    public abstract class EntityBase : INotifyPropertyChanging, INotifyPropertyChanged
    {
        public event PropertyChangingEventHandler PropertyChanging;
        public event PropertyChangedEventHandler PropertyChanged;

        private EntityState state = EntityState.NotChanged;

        public EntityState State
        {
            get { return state; }
            set { state = value; }
        }

        public EntityBase()
        {
            state = EntityState.NotChanged;
        }

        protected virtual void SendPropertyChanging(string propertyName)
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }
        protected virtual void SendPropertyChanged(string propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }

When one of your employes in collection changed - mark it with EntityState.Changed, and then you can request only changed entities from collection and process them as you want.

Eugene Cheverda
Thanks for your help/reply.Are you saying hook the CollectionChanged Event of the observableCollection and then mark you item as dirty in thereis that what you mean?
Place were to set this property you choose by yourself. May be in setters, may be on custom PropertyChangedCallback's methods. The idea is: when some property of object changed - mark that object as changed. The things are very simple, and you don't need to post ALL your collection to DB, for example, in order to update it, when only 1% of entities changed.
Eugene Cheverda
That is exactly what I want to achieve.Let me have a go and come back t you.
HiAdd a property EntityState as you described and then set the State=Changed in the setter When I load my employees I Set all to Added.However even when I change one only it still says I have changed them all. Since I user is editing the datagrid and then press save need to find event that can track which employee has changed and set to "Changed".Am I complicating things and missing something very simple?
When you load entities, they have to be set to NotChanged. When your app add entity, then you set Added, if delete - Deleted. If property changes - Changed. Then when you are going to commit changes, you perform corresponding action to Entities on you've worked on. For example, when you change name of employee in TextBox of your list - set to state to Changed. Then commit all states except of NotChanged.
Eugene Cheverda
Hi,Sorry for delay in answering trying to put together a noddy example to see if all works as you say.It all works apart from when a user edit a cell.I cannot find a way to trap when a user modify a cell and mark it changed
Seem to all work now in my noddy example .Thanks for all your help!
A: 

I'm not aware of any built in way to do what you want.

What I've done in the past is to implement a boolean IsDirty property on the objects in the collection. Then set the IsDirty property to true anytime you raise PropertyChanged.

Scrappydog