MVVM - implementing 'IsDirty' functionality to a ModelView in order to save data

前端 未结 7 1447
攒了一身酷
攒了一身酷 2021-02-04 07:13

Being new to WPF & MVVM I struggling with some basic functionality.

Let me first explain what I am after, and then attach some example code...

I have a scree

相关标签:
7条回答
  • 2021-02-04 08:10

    I've done some work on implementing IsDirty for models that is wrapped in my ViewModel.

    The result really simplified my ViewModels:

    public class PersonViewModel : ViewModelBase
    {
        private readonly ModelDataStore<Person> data;
        public PersonViewModel()
        {
            data = new ModelDataStore<Person>(new Person());
        }
    
        public PersonViewModel(Person person)
        {
            data = new ModelDataStore<Person>(person);
        }
    
        #region Properties
    
        #region Name
        public string Name
        {
            get { return data.Model.Name; }
            set { data.SetPropertyAndRaisePropertyChanged("Name", value, this); }
        }
        #endregion
    
        #region Age
        public int Age
        {
            get { return data.Model.Age; }
            set { data.SetPropertyAndRaisePropertyChanged("Age", value, this); }
        }
        #endregion
    
        #endregion
    }
    

    Code @ http://wpfcontrols.codeplex.com/ Check under the Patterns assembly and MVVM folder, you'll find a ModelDataStore class.

    P.S. I haven't done a full scale test on it, just the really simple test you'll find the Test assembly.

    0 讨论(0)
提交回复
热议问题