0

I have Model Class Employee:

public class Employee   
{
    public string Id { get; set; }  
    public string Name { get; set; }  
    public string LastName { get; set; }            
}

and View Model class EmployeeViewModel which contains an Employee Object

public class EmployeeViewModel : INotifyPropertyChanged  
{  
    public EmployeeViewModel()
    {

    }            

    private Employee currentEmployee;

    public Employee CurrentEmployee
    {
        get { return this.currentEmployee; }

        set
        {
            this.currentEmployee = value;
            this.NotifyPropertyChanged("CurrentEmployee");
        }
    }

    //Some code .....
    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

        #endregion
}

Now the View (WPF) will use Employee object in the View Model as ItemSource to display Employee data

Now the question is: I have Update button on the view and when I change the Employee properties in the view (via text boxes) I want to update the model (so afterward i can update the database), how to update this model from the view.

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Hussein
  • 945
  • 1
  • 12
  • 30
  • if you used binding to the CurrentEmployee the data should be there via the binding , consider implementing INotifyPropertyChanged at the model , do you know how to get the button's command(aka ICommand) ?share xaml for better answers – ZSH Jan 02 '13 at 12:22

2 Answers2

1

As I checked there something weird with your Model Class. It is the one that should implement INotifyPropertyChanged then create backing fields for each property something like this.

Model Class

public class Employee:INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name= value;
                OnPropertyChanged("Name");
            }

        }
          #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }

**ViewModel Class**

 class EmployeeViewModel 
    {
        private IList<Employee> _employees;
        public EmployeeViewModel()
        {
            _employees= new List<Employee>
            {
                new Employee{ID=1, Name ="Emp1"},
                new Employee{ID=2, Name="Emp2"}
            };


        }

        public IList<Employee> Employees
        {
            get
            {
                return _employees;
            }
            set
            {
                _employees= value;
            }
        }

        private ICommand mUpdater;
        public ICommand UpdateCommand
        {
            get
            {
                if (mUpdater == null)
                    mUpdater = new Updater();
                return mUpdater;
            }
            set
            {
                mUpdater = value;
            }
        }

        private class Updater : ICommand
        {
            #region ICommand Members

            public bool CanExecute(object parameter)
            {
                return true;
            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object parameter)
            {

            }

            #endregion
        }

    }

You may put your logic inside OnPropertyChanged event. This method is always called whenever you made changes on the UI

Jobert Enamno
  • 4,403
  • 8
  • 41
  • 63
  • thanks alot for your follow up.Firstly i want to use clean model following the DDD in this point. Secondly I applied this solution for sake of solving the problem but always the model not updated even after implementing INotifyPropertyChanged in Model and View Model both. – Hussein Jan 03 '13 at 18:56
0

If you are using ObservableCollection modify your List by searching the item based on ID then if found do the modification of values. Whatever changes you have made will automatically affect the UI items if they are bound to your ObservableCollection then use the modified collection to update your DB records

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Jobert Enamno
  • 4,403
  • 8
  • 41
  • 63
  • but the view displays the details for one emplyee at a time, then in the view i need to modify some employee property like name or so then i want the employee model to catch this change so i can use repository to submit change into DB – Hussein Jan 02 '13 at 17:30