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

前端 未结 7 1454
攒了一身酷
攒了一身酷 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:00

    This is how I have implemented IsDirty. Create a wrapper for every property of User class (inheriting User class with IPropertyChanged and implementing onpropertychanged in User class wont help) in your ViewModal. You need to change your binding from UserName to WrapUserName.

    public string WrapUserName 
        {
            get
            {
                return User.UserName          
            }
            set
            {
                User.UserName = value;
                OnPropertyChanged("WrapUserName");
            }
        }
    

    Now have a property

     public bool isPageDirty
        {
            get;
            set;
        }     
    

    Since your viewmodal inherits from baseviewmodal and baseviewmodal implements onPropertyChanged.

    UserViewModel.PropertyChanged += (s, e) => { isPageDirty = true; };    
    

    In case any of the propertychanges,isPageDirty will be true, So while saving you chan check isPageDirty.

提交回复
热议问题