如何从视图更新模型

时间:2013-01-02 12:01:28

标签: wpf mvvm inotifypropertychanged itemsource

我有Model Class Employee:

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

和查看模型类EmployeeViewModel,其中包含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
}

现在,视图(WPF)将使用视图模型中的Employee对象ItemSource来显示员工数据

现在问题是:我在视图上有更新按钮,当我在视图中更改Employee属性时(通过文本框)我想更新模型(所以之后我可以更新数据库),如何从视图中更新此模型。

2 个答案:

答案 0 :(得分:1)

当我检查你的模型类时有些奇怪。应该实现INotifyPropertyChanged,然后为每个属性创建这样的支持字段。

模型类

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
        }

    }

您可以将您的逻辑放在OnPropertyChanged事件中。每当您在UI上进行更改时,都会调用此方法

答案 1 :(得分:0)

如果您使用ObservableCollection修改List,请根据ID搜索项目,如果找到则修改值。如果UI项目绑定到您的ObservableCollection,那么您所做的任何更改都将自动影响UI项目,然后使用修改后的集合来更新您的数据库记录