INotifyPropertyChanged通知,mvvm更新数据

INotifyPropertyChanged

在WPF MVVM模式开发中,使用他可以通知数据更新

  1. 定义一个NotifyBase工具类,继承INotifyPropertyChanged

    public class NotifyBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void DoNotify([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(name));
        }
    }
    
  2. 在绑定的Model中继承定义好的NotifyBase

     public class SongModel : NotifyBase
     {
         /// 
         /// 歌曲url
         /// 
         private string _songUrl;
         public string SongUrl
         {
             get { return this._songUrl; }
             set
             {
                 _songUrl = value;
                 DoNotify();
             }
         }
    
         /// 
         /// 本地下载后的mp3路径
         /// 
         private string _localSongUrl;
         public string LocalSongUrl
         {
             get { return this._localSongUrl; }
             set
             {
                 _localSongUrl = value;
                 DoNotify();
             }
         }
    
         /// 
         /// 歌曲图片
         /// 
         private string _picUrl;
         public string PicUrl
         {
             get { return this._picUrl; }
             set
             {
                 _picUrl = value;
                 DoNotify();
             }
         }
    
    
     }
    

你可能感兴趣的:(winfrom,wpf,c#,开发语言)