mahboub_mo
mahboub_mo

Reputation: 3038

How do i detect a WPFDataGrid row Data changed?

I have a DataGrid,and need to detect when a user has make changes to a row.I don't want to use CellEditEnding because whenever a row get focus and lost it without any inputs,this event get raised,in the other way i need to bind a bool property to each row that set to true when the row got chgangd.

Upvotes: 2

Views: 2422

Answers (2)

Bolu
Bolu

Reputation: 8786

Use following code as an example, so you know the basic idea of how to trace if an item in your ItemSource had been changed (here only compared to the initial value only).

List<myItem> Items=new List<myItem>(); //your ItemSource



    class myItem:ObservableObject //an class implement INotifyPropertyChanged interface
    {
        string _inititemName;
        string _itemName;
        bool itemChanged; //here is your indicator

        myItem(string name)
        {
            _inititemName=itemName=name;
        }

        public string itemName
        {   
            get{return _itemName;}
            set
            {  
                _itemName=vlaue; 
                if (_itemName!=_inititemName) 
                   itemChanged=true; 
                else 
                   itemChanged=false;
                RaisePropertyChanged("itemName"); //or whatever the name of the method is that invoke OnPropertyChanged
            }
        }
    } 

Upvotes: 1

Peregrine
Peregrine

Reputation: 4546

Make the properties of your item class set a boolean update flag when they are modified

e.g.

public class MyGridItem
{
    public MyGridItem(string Name)
    { 
        this.Name = Name;
        Updated = false;
    }

    public bool Updated {get; private set;}

    private string _Name = null;
    public string Name
    {
        get { return _Name; }
        set { 
                if (!_Name.Equals( value ))
                {
                    _Name = value;
                    Updated = true
                 }
            }
    }
}

Upvotes: 0

Related Questions